package com.xbongbong.pro.statistic.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.BiConstant;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.SizeConstant;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.PaasFormRefEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.domain.entity.ext.StatisticPaasFormDataEsEntity;
import com.xbongbong.paas.elasticsearch.constant.EsMappingConstant;
import com.xbongbong.paas.elasticsearch.constant.EsNormalConstant;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.StatisticBiFormDataEntity;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.BIProductReformEnum;
import com.xbongbong.paas.enums.BITeamEnum;
import com.xbongbong.paas.enums.BiEsAliasEnum;
import com.xbongbong.paas.enums.BiRelTableEnum;
import com.xbongbong.paas.enums.EsAliasEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.SubFieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.model.PaasFormRefModel;
import com.xbongbong.paas.pojo.dto.PaasFormDataEsInitDTO;
import com.xbongbong.paas.pojo.dto.PaasFormDataEsMappingDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsInitVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisConstant;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.statistics.pojo.dto.SlaveListDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.ReflectHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CloneUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.MD5Util;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.domain.entity.BiDataCorrEntity;
import com.xbongbong.pro.domain.entity.BiDataCorrelationEntity;
import com.xbongbong.pro.domain.entity.BiProWareEntity;
import com.xbongbong.pro.domain.entity.BiProWarehouseEntity;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.enums.ProVersionEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.StatisticsServicesErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BiErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.service.toolbox.statistic.help.TeamHelp;
import com.xbongbong.pro.statistic.constant.ChartConstant;
import com.xbongbong.pro.statistic.enums.ChartLimitTypeEnum;
import com.xbongbong.pro.statistic.enums.ChartTypeEnum;
import com.xbongbong.pro.statistic.enums.FormLocationEnum;
import com.xbongbong.pro.statistic.enums.StatisticsTypeEnum;
import com.xbongbong.pro.statistic.help.CustomHelp;
import com.xbongbong.pro.statistic.help.InitMessageQueueHelp;
import com.xbongbong.pro.statistic.model.BiProWareModel;
import com.xbongbong.pro.statistic.model.BiProWarehouseModel;
import com.xbongbong.pro.statistic.model.ChartCustomModel;
import com.xbongbong.pro.statistic.pojo.LogStashInputPojo;
import com.xbongbong.pro.statistic.pojo.LogStashOutputPojo;
import com.xbongbong.pro.statistic.pojo.SlaveSourcePojo;
import com.xbongbong.pro.statistic.pojo.WareTablePojo;
import com.xbongbong.pro.statistic.pojo.dto.ChartEditDTO;
import com.xbongbong.pro.statistic.pojo.dto.InitAllDTO;
import com.xbongbong.pro.statistic.pojo.dto.LogStashEditDTO;
import com.xbongbong.pro.statistic.pojo.dto.MappingInitDTO;
import com.xbongbong.pro.statistic.pojo.dto.ReInitDataDTO;
import com.xbongbong.pro.statistic.pojo.dto.StructureBiInsertDTO;
import com.xbongbong.pro.statistic.pojo.dto.UpdateDataDTO;
import com.xbongbong.pro.statistic.pojo.vo.DataInitVO;
import com.xbongbong.pro.statistic.service.BiProWareService;
import com.xbongbong.pro.statistic.service.ChartLimitService;
import com.xbongbong.pro.statistic.service.DataWarehouseService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StockConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.BiUserTeamEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.InstockTypeEnum;
import com.xbongbong.saas.enums.OutstockTypeEnum;
import com.xbongbong.saas.enums.SheetAloneEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.InstockEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.help.InitDataHelp;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import com.xbongbong.saas.service.toolbox.help.UpdatePackageRedisService;
import org.apache.http.HttpEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2019/2/12 14:10
 * @since v1.0
 */
@Service("dataWarehouseService")
public class DataWarehouseServiceImpl implements DataWarehouseService {
    private static final Logger LOG = LoggerFactory.getLogger(DataWarehouseServiceImpl.class);
    @Resource
    private ChartCustomModel chartCustomModel;
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private InitDataHelp initDataHelp;
    @Resource
    private BiProWarehouseModel biProWarehouseModel;
    @Resource
    private BiProWareModel biProWareModel;
    @Resource
    private CustomHelp customHelp;
    @Resource
    private TeamHelp teamHelp;
    @Resource
    private InitMessageQueueHelp initMessageQueueHelp;
    @Resource
    private ThreadPoolTaskExecutor biInitThreadPool;
    @Resource
    private ThreadPoolTaskExecutor biUpdateThreadPool;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private BiProWareService biProWareService;
    @Resource
    private UpdatePackageRedisService updatePackageRedisService;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private PaasFormRefModel paasFormRefModel;
    @Resource
    private ChartLimitService chartLimitService;
    @Resource
    private IndexTypeModel indexTypeModel;
    @Resource
    private PackageHelp packageHelp;

    @Override
    public DataInitVO initData(ChartEditDTO chartEditDTO) throws XbbException {
        return initBiData(chartEditDTO);
    }

    public DataInitVO initBiData(ChartEditDTO chartEditDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        String corpid = chartEditDTO.getCorpid();
        ChartEntity chartEntity = chartCustomModel.getByKey(chartEditDTO.getChartId(), corpid);
        JSONObject driverSource = chartEntity.getDriverSources();
        JSONArray slaveSources = chartEntity.getSlaveSources();
        Integer statisticsType = chartEntity.getStatisticsType();
        Integer chartType = chartEntity.getChartType();
        //首先确定是自定义
        if(!Objects.equals(StatisticsTypeEnum.CUSTOM_STATISTIC.getCode(),statisticsType)){
            throw  new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224019);
        }
        //数值指标，指标都是单表的，直接去查询就好
        if(!Objects.equals(ChartTypeEnum.VALUE.getCode(),chartType)){
            //多表关联需要建仓
            if(Objects.equals(chartEntity.getMuti(), BasicConstant.ZERO)){
                boolean  hasSecondSlave = false;
                if(slaveSources.size() > 1){
                    hasSecondSlave = true;
                }
                SlaveSourcePojo driverSourcePojo = JSON.toJavaObject(driverSource, SlaveSourcePojo.class);
                List<SlaveSourcePojo> slaveSourceList = slaveSources.toJavaList(SlaveSourcePojo.class);
                customHelp.setSourcePojo(chartEntity, driverSourcePojo, slaveSourceList);
                Integer businessType = driverSourcePojo.getBusinessType();
                Integer saasMark = driverSourcePojo.getSaasMark();
                BiRelTableEnum biRelTableEnum  = customHelp.getBiRelTableEnum(businessType, saasMark);
                long now = DateUtil.getInt();
                List<SlaveSourcePojo> slaveSourceListForCorrelation = new ArrayList<>();
                try {
                    BeanUtil.copyPropertiesList(slaveSourceList,slaveSourceListForCorrelation,SlaveSourcePojo.class);
                } catch (Exception e) {
                    throw  new XbbException(BiErrorCodeEnum.API_ERROR_241009);
                }
                String  tableSerial = biProWareService.getTableSerial(chartEntity);
                List<BiProWareEntity> list = biProWareService.getBiProWareEntities(corpid,tableSerial);
                BiProWareEntity biProWareEntity = null;
                boolean initFlag;
                if (list != null && !list.isEmpty()) {
                    //原来是否存在数据源(被删除过的，上面已经判断过没有被删除的)
                    biProWareEntity  = list.get(0);
                    biProWareEntity.setIsDone(1);
                    biProWareEntity.setUpdateTime(now);
                    //原来删除的关联应该从一开始
                    if(Objects.equals(biProWareEntity.getDel(),1)){
                        biProWareEntity.setIsCorrelation(1);
                        biProWareEntity.setDel(0);
                        initFlag = false;
                    }else {
                        biProWareEntity.setIsCorrelation(biProWareEntity.getIsCorrelation() + 1);
                        initFlag = true;
                    }
                    biProWareModel.update(biProWareEntity);
                }else {
                    initFlag = false;
                    //新建
                    biProWareEntity  = new BiProWareEntity();
                    biProWareEntity.setCorpid(corpid);
                    biProWareEntity.setRelTable( biRelTableEnum.getCode());
                    //数据完成之后再给done = 1
                    biProWareEntity.setIsDone(0);
                    biProWareEntity.setDel(0);
                    biProWareEntity.setAddTime(now);
                    biProWareEntity.setUpdateTime(now);
                    biProWareEntity.setTableSerial(tableSerial);
                    biProWareEntity.setFormIdAttr(chartEntity.getFormIdAttr());
                    //TODO 考虑这里会不会出问题
                    biProWareEntity.setAtJson(driverSource);
                    //allCorrelationListCopy是排过序的
                    for (int i = 0; i < slaveSourceList.size(); i++) {
                        SlaveSourcePojo slaveSourcePojo = slaveSourceList.get(i);
                        WareTablePojo  wareTablePojo = new WareTablePojo();
                        wareTablePojo.setFormId(slaveSourcePojo.getFormId());
                        wareTablePojo.setSaasMark(slaveSourcePojo.getSaasMark());
                        wareTablePojo.setFieldType(slaveSourcePojo.getFieldType());
                        wareTablePojo.setBusinessType(slaveSourcePojo.getBusinessType());
                        wareTablePojo.setInData(slaveSourcePojo.getInData());
                        wareTablePojo.setLinkAttr(slaveSourcePojo.getLinkAttr());
                        wareTablePojo.setLinkAttrCopy(slaveSourcePojo.getLinkAttrCopy());
                        //slaveSourceList 是从0开始的，只有从表，size最大5，但是最大4
                        FormLocationEnum formLocationEnum = FormLocationEnum.getByCode(i + 2);
                        if(formLocationEnum == null){
                            continue;
                        }
                        switch (formLocationEnum) {
                            case B:
                                biProWareEntity.setBtJson(JSON.parseObject(JSON.toJSON(wareTablePojo).toString()));
                                break;
                            case C:
                                biProWareEntity.setCtJson(JSON.parseObject(JSON.toJSON(wareTablePojo).toString()));
                                break;
                            case D:
                                biProWareEntity.setDtJson(JSON.parseObject(JSON.toJSON(wareTablePojo).toString()));
                                break;
                            case E:
                                biProWareEntity.setEtJson(JSON.parseObject(JSON.toJSON(wareTablePojo).toString()));
                                break;
                            case F:
                                biProWareEntity.setFtJson(JSON.parseObject(JSON.toJSON(wareTablePojo).toString()));
                                break;
                            default:
                                break;
                        }
                    }
                    //TODO 初始化肯定是有关联需求才会触发，设置为1
                    biProWareEntity.setIsCorrelation(1);
                    biProWareModel.insert(biProWareEntity);
                }
                //数据源已存在
//                if(initFlag) {
                if(initFlag || chartLimitService.getCkFlag()) {
                    return dataInitVO;
                }
                boolean finalHasSecondSlave = hasSecondSlave;
                BiProWareEntity finalBiProWarehouseEntity = biProWareEntity;
                Boolean startRun = chartEditDTO.getStartRun();
                if(startRun == null){
                    startRun = false;
                    //本地默认不起线程
                    String dir = ProBaseConfig.getProjectName();
                    if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                        startRun = true;
                    }
                }
                if(!startRun){
                    Runnable runnable = new Runnable(){
                        @Override
                        public void run() {
                            //初始化数据
                            try {
                                sendbBiData(corpid, finalHasSecondSlave, driverSourcePojo, slaveSourceList, businessType, saasMark, biRelTableEnum, slaveSourceListForCorrelation, finalBiProWarehouseEntity,chartEntity);
                                //因为更新依赖correlation ，所以放在数据完成后执行
                                biProWareModel.updateIsDoneByKey(finalBiProWarehouseEntity.getId(),corpid);
                                paasRedisHelper.setValue(RedisPrefixConstant.BUILD_WARE_FLAG, chartEntity.getCorpid() + "_" + chartEntity.getId(), System.currentTimeMillis(), TimeConstant.TINY_DURATION);
                                paasRedisHelper.setValue(RedisPrefixConstant.BUILD_WARE_FLAG_ONE_MIN, chartEntity.getCorpid() + "_" + chartEntity.getId(), System.currentTimeMillis(), TimeConstant.SECONDS_PER_MINUTE);
                            } catch (Exception e) {
                                LOG.error(BiErrorCodeEnum.API_ERROR_241006.getMsg()+"====="+finalBiProWarehouseEntity.getId(),e);
                            }
                        }
                    };
                    biInitThreadPool.execute(runnable);
                }else {
                    //初始化数据
                    try {
                        sendbBiData(corpid, finalHasSecondSlave, driverSourcePojo, slaveSourceList, businessType, saasMark, biRelTableEnum, slaveSourceListForCorrelation, finalBiProWarehouseEntity,chartEntity);
                        //因为更新依赖correlation ，所以放在数据完成后执行
                        biProWareModel.updateIsDoneByKey(finalBiProWarehouseEntity.getId(),corpid);
                        paasRedisHelper.setValue(RedisPrefixConstant.BUILD_WARE_FLAG, chartEntity.getCorpid() + "_" + chartEntity.getId(), System.currentTimeMillis(), TimeConstant.TINY_DURATION);
                        paasRedisHelper.setValue(RedisPrefixConstant.BUILD_WARE_FLAG_ONE_MIN, chartEntity.getCorpid() + "_" + chartEntity.getId(), System.currentTimeMillis(), TimeConstant.SECONDS_PER_MINUTE);
                    } catch (Exception e) {
                        LOG.error(BiErrorCodeEnum.API_ERROR_241006.getMsg()+"====="+finalBiProWarehouseEntity.getId(),e);
                    }
                }
            }
        }
        return dataInitVO;
    }

    /**
     * 根据warehouseId 删除数据
     * @param warehouseId
     * @param index
     * @throws IOException
     * @throws XbbException
     */
    @Override
    public void deleteQueryByWareHouseId(Long warehouseId, String index) throws IOException, XbbException {
        DeleteByQueryRequest request = new DeleteByQueryRequest(index);
        request.setConflicts("proceed");
        request.setQuery(new TermQueryBuilder("warehouseId", warehouseId));
        xbbElasticsearchRestTemplate.getRestHighLevelClient(index).deleteByQuery(request, RequestOptions.DEFAULT);
    }

    @Override
    public DataInitVO reInitBIData(ReInitDataDTO reInitDataDTO) throws XbbException {
        try {
            DataInitVO dataInitVO = new DataInitVO();
            String corpid = reInitDataDTO.getCorpid();
            ChartEntity chartEntity = chartCustomModel.getByKey(reInitDataDTO.getChartId(), corpid);
            if(chartEntity == null){
                dataInitVO.setMessage(I18nMessageUtil.getMessage(I18nStringConstant.ENTER_CORRECT_DATA1));
                return dataInitVO;
            }
            if(chartLimitService.getCkFlag()){
                //这个接口只有运维人员使用，不用国际化
                dataInitVO.setMessage("当前公司数据源切换至CK不需要再刷表========="+corpid);
                return dataInitVO; 
            }
            // 加上图表限制
            List<SlaveSourcePojo> selectList = new ArrayList<>();
            selectList.add(chartEntity.getDriverSources().toJavaObject(SlaveSourcePojo.class));
            JSONArray slaveSources = chartEntity.getSlaveSources();
            Integer limitNum;
            if(CollectionsUtil.isNotEmpty(slaveSources)){
                selectList.addAll(slaveSources.toJavaList(SlaveSourcePojo.class));
                limitNum = commonHelp.getChartLimit(corpid,ChartLimitTypeEnum.BI_MUITI_DATA_NUM_LIMIT.getAlias());
            }else {
                limitNum = commonHelp.getChartLimit(corpid,ChartLimitTypeEnum.BI_DATA_NUM_LIMIT.getAlias());
            }
            updatePackageRedisService.chartLimit(selectList, corpid,limitNum);
            return reInitBIData(dataInitVO, corpid, chartEntity);
        } catch (Exception e) {
            LOG.error("==========",e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public DataInitVO reInitAllBIData(ReInitDataDTO reInitDataDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del",0);
        param.put("statisticsType",2);
        List<ChartEntity> list = chartCustomModel.findMutiEntitys(param);
        if(CollectionsUtil.isEmpty(list)){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
        }
        LOG.info("========开始处理==="+DateUtil.getString());
        for (ChartEntity chartEntity : list) {
            try {
                reInitBIData(dataInitVO, chartEntity.getCorpid(), chartEntity);
            } catch (Exception e) {
                LOG.error("",e);
            }
        }
        return dataInitVO;
    }

    @Override
    public DataInitVO reInitCustomerBIData(MappingInitDTO mappingInitDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del",0);
        param.put("muti",0);
        List<ChartEntity> list = chartCustomModel.findEntitys(param);
        if(CollectionsUtil.isEmpty(list)){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
        }
        LOG.info("========开始处理==="+DateUtil.getString());
        for (int i = 0; i < list.size(); i++) {
            ChartEntity chartEntity = list.get(i);
            try {
                reInitCustomerBIData(dataInitVO, chartEntity.getCorpid(), chartEntity,mappingInitDTO.getCode());
                LOG.info("size========" + list.size()+"==============="+i);
            } catch (Exception e) {
                LOG.error("",e);
            }
        }

        return dataInitVO;
    }

    private DataInitVO reInitCustomerBIData(DataInitVO dataInitVO, String corpid, ChartEntity chartEntity,Integer code) throws Exception {
        JSONArray slaveSources = chartEntity.getSlaveSources();
        Integer statisticsType = chartEntity.getStatisticsType();
        Integer chartType = chartEntity.getChartType();
        //首先确定是自定义
        if(!Objects.equals(StatisticsTypeEnum.CUSTOM_STATISTIC.getCode(),statisticsType)){
            throw  new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224019);
        }
        //数值指标，指标都是单表的，直接去查询就好
        if(!Objects.equals(ChartTypeEnum.VALUE.getCode(),chartType)){
            //多表关联需要建仓
            if(!EsUtil.getSingle(chartEntity)){
                List<BiProWareEntity> list = biProWareService.getBiProWareEntities(chartEntity);
                if (list == null  || list.isEmpty()) {
                    dataInitVO.setMessage(I18nMessageUtil.getMessage(I18nStringConstant.DATA_WAREHOUSE_NOT_EXIST));
                    return dataInitVO;
                }
                //先查数据保证数据仓无数据
                BiProWareEntity biProWareEntity = list.get(0);
                //TODO
                if(Objects.equals(biProWareEntity.getRelTable(), code)){
                    Long warehouseId = biProWareEntity.getId();
                    if (warehouseId == null || warehouseId == 0) {
                        dataInitVO.setMessage(I18nMessageUtil.getMessage(I18nStringConstant.DATA_WAREHOUSE_NOT_EXIST));
                        return dataInitVO;
                    }
                    BiRelTableEnum biRelTableEnum = BiRelTableEnum.getByCode(biProWareEntity.getRelTable());
                    String index = biRelTableEnum.getIndex();
                    BoolQueryBuilder boolQueryBuilder = boolQuery();
                    boolQueryBuilder.filter(termQuery("warehouseId", warehouseId));
                    boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, biRelTableEnum.getType()));
                    CountRequest countRequest = new CountRequest(index);
                    countRequest.query(boolQueryBuilder);
                    long count = xbbElasticsearchRestTemplate.count(countRequest);
                    if (count > 0) {
                        dataInitVO.setMessage(I18nMessageUtil.getMessage(I18nStringConstant.DELETE_DATA_REBUILD));
                        return dataInitVO;
                    }
                    SlaveSourcePojo driverSourcePojo = chartEntity.getDriverSources().toJavaObject(SlaveSourcePojo.class);
                    List<SlaveSourcePojo> slaveSourceListForCorrelation = new ArrayList<>();
                    List<SlaveSourcePojo> slaveSourceList = slaveSources.toJavaList(SlaveSourcePojo.class);
                    boolean hasSecondSlave = false;
                    try {
                        BeanUtil.copyPropertiesList(slaveSourceList, slaveSourceListForCorrelation, SlaveSourcePojo.class);
                    } catch (Exception e) {
                        throw new XbbException(BiErrorCodeEnum.API_ERROR_241009);
                    }
                    if (slaveSources.size() > 1) {
                        hasSecondSlave = true;
                    }
                    sendbBiData(corpid, hasSecondSlave, driverSourcePojo, slaveSourceList, driverSourcePojo.getBusinessType(), driverSourcePojo.getSaasMark(), biRelTableEnum, slaveSourceListForCorrelation, biProWareEntity, chartEntity);
                    LOG.info("reInitDatasucess========" + System.currentTimeMillis());
                }
            }
        }
        return dataInitVO;
    }
    
    
    private DataInitVO reInitBIData(DataInitVO dataInitVO, String corpid, ChartEntity chartEntity) throws Exception {
        JSONArray slaveSources = chartEntity.getSlaveSources();
        Integer statisticsType = chartEntity.getStatisticsType();
        Integer chartType = chartEntity.getChartType();
        //首先确定是自定义
        if(!Objects.equals(StatisticsTypeEnum.CUSTOM_STATISTIC.getCode(),statisticsType)){
            throw  new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224019);
        }
        //数值指标，指标都是单表的，直接去查询就好
        if(!Objects.equals(ChartTypeEnum.VALUE.getCode(),chartType)){
            //多表关联需要建仓
            if(!EsUtil.getSingle(chartEntity)){
                List<BiProWareEntity> list = biProWareService.getBiProWareEntities(chartEntity);
                if (list == null  || list.isEmpty()) {
                    dataInitVO.setMessage(I18nMessageUtil.getMessage(I18nStringConstant.DATA_WAREHOUSE_NOT_EXIST));
                    return dataInitVO;
                }
                //先查数据保证数据仓无数据
                BiProWareEntity biProWareEntity = list.get(0);
                Long warehouseId = biProWareEntity.getId();
                if (warehouseId == null  || warehouseId == 0) {
                    dataInitVO.setMessage(I18nMessageUtil.getMessage(I18nStringConstant.DATA_WAREHOUSE_NOT_EXIST));
                    return dataInitVO;
                }
                BiRelTableEnum biRelTableEnum =  BiRelTableEnum.getByCode(biProWareEntity.getRelTable());
                String index = biRelTableEnum.getIndex();
                HttpEntity httpEntity = null;
                try {
                    deleteQueryByWareHouseId(warehouseId,index);
                    LOG.info("deling========"+ System.currentTimeMillis());
                } catch (IOException e) {
                    LOG.error("reInitDataError",e);
                    throw  new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
                } finally {
                    if (Objects.nonNull(httpEntity)) {
                        try {
                            ((NStringEntity) httpEntity).close();
                        }catch (Exception e) {
                            LOG.error("reInitDataError",e);
                        }
                    }
                }
                BoolQueryBuilder boolQueryBuilder = boolQuery();
                boolQueryBuilder.filter(termQuery("warehouseId", warehouseId));
                boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,biRelTableEnum.getType()));
                CountRequest countRequest = new CountRequest(index);
                countRequest.query(boolQueryBuilder);
                long count = xbbElasticsearchRestTemplate.count(countRequest);
                if(count > 0){
                    dataInitVO.setMessage(I18nMessageUtil.getMessage(I18nStringConstant.DELETE_DATA_REBUILD));
                    return dataInitVO;
                }
                SlaveSourcePojo driverSourcePojo = chartEntity.getDriverSources().toJavaObject(SlaveSourcePojo.class);
                List<SlaveSourcePojo> slaveSourceListForCorrelation = new ArrayList<>();
                List<SlaveSourcePojo> slaveSourceList = slaveSources.toJavaList(SlaveSourcePojo.class);
                boolean hasSecondSlave = false;
                try {
                    BeanUtil.copyPropertiesList(slaveSourceList,slaveSourceListForCorrelation,SlaveSourcePojo.class);
                } catch (Exception e) {
                    throw  new XbbException(BiErrorCodeEnum.API_ERROR_241009);
                }
                if(slaveSources.size() > 1){
                    hasSecondSlave = true;
                }
                sendbBiData(corpid, hasSecondSlave, driverSourcePojo, slaveSourceList, driverSourcePojo.getBusinessType(), driverSourcePojo.getSaasMark(), biRelTableEnum, slaveSourceListForCorrelation, biProWareEntity,chartEntity);
                //原来可能没有创建完数据没有走到成功更改标记
                if (Objects.equals(biProWareEntity.getIsDone(), 0)) {
                    biProWareModel.updateIsDoneByKey(warehouseId,corpid);
                    LOG.info("==================="+ warehouseId);
                }
                LOG.info("reInitDatasucess========"+ System.currentTimeMillis());
            }
        }
        return dataInitVO;
    }

    @Override
    public DataInitVO initAllBiMapping(MappingInitDTO mappingInitDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        try {
            for (BiRelTableEnum biRelTableEnum : BiRelTableEnum.values()) {
                if(Objects.equals(biRelTableEnum.getRefCode(),-1)){
                    continue;
                }
                try {
                    xbbElasticsearchRestTemplate.deleteIndex(biRelTableEnum.getIndex());
                } catch (Exception e) {
                    LOG.error(BiErrorCodeEnum.API_ERROR_241011.getMsg(),e);
                }
                try {
                    LOG.info("=====开始========="+biRelTableEnum.getIndex());
                    initBiMapping(biRelTableEnum);
                    LOG.info("====完成========="+biRelTableEnum.getIndex());
                } catch (XbbException e) {
                    LOG.error(BiErrorCodeEnum.API_ERROR_241011.getMsg(),e);
                }
            }
        } catch (Exception e) {
            LOG.error(BiErrorCodeEnum.API_ERROR_241011.getMsg(),e);
        }
        return dataInitVO;
    }

    @Override
    public DataInitVO initNewBiMapping(MappingInitDTO mappingInitDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        try {
            BiRelTableEnum biRelTableEnum = BiRelTableEnum.getByCode(mappingInitDTO.getCode());
            if(biRelTableEnum == null ){
                throw new XbbException(BiErrorCodeEnum.API_ERROR_241010);
            }
            try {
                xbbElasticsearchRestTemplate.deleteIndex(biRelTableEnum.getIndex());
            } catch (Exception e) {
                //TODO 已存在则跳过
                LOG.error(BiErrorCodeEnum.API_ERROR_241011.getMsg(),e);
            }
            initBiMapping(biRelTableEnum);
        } catch (Exception e) {
            LOG.error(BiErrorCodeEnum.API_ERROR_241011.getMsg(),e);
            throw  new XbbException(BiErrorCodeEnum.API_ERROR_241011);
        }
        return dataInitVO;
    }

    @Override
    public DataInitVO updateBIData(UpdateDataDTO updateDataDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        String redisValue = paasRedisHelper.getValue(RedisPrefixConstant.BI_UPDATE_KEY+BiConstant.INDEX_SUFFIX, updateDataDTO.getXbbCode());
        if(!StringUtil.isEmpty(redisValue)){
            dataInitVO.setMessage("等着吧");
            //TODO  记得放开
            return  dataInitVO;
        }
        paasRedisHelper.setValue(RedisPrefixConstant.BI_UPDATE_KEY+BiConstant.INDEX_SUFFIX,updateDataDTO.getXbbCode(),updateDataDTO.getXbbCode(), RedisConstant.SHORT_DURATION);
        LOG.info("=====updateStart========更新开始=========="+DateUtil.getInt());
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("del", 0);
        //关联大于0
        params.put("isCorrelationGT", 0);
        //已完成
        params.put("isDone", 1);
        //
        List<BiProWareEntity>  biProWareEntityList = biProWareModel.findEntitys(params);
        if(CollectionsUtil.isEmpty(biProWareEntityList)){
            return dataInitVO;
        }
        String dir = ProBaseConfig.getProjectName();
        List<SlaveSourcePojo> selectList = null;
        if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
            for (BiProWareEntity biProWareEntity : biProWareEntityList) {
                if( chartLimitService.getCkFlag()){
                    LOG.error("当前公司数据源已经切换至CK=============="+biProWareEntity.getCorpid());
                    continue;
                }
                try {
                    selectList = new ArrayList<>();
                    selectList.add(biProWareEntity.getAtJson().toJavaObject(SlaveSourcePojo.class));
                    if(biProWareEntity.getBtJson() != null && biProWareEntity.getBtJson().size() >0 ){
                        selectList.add(biProWareEntity.getBtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getCtJson() != null && biProWareEntity.getCtJson().size() >0 ){
                        selectList.add(biProWareEntity.getCtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getDtJson() != null && biProWareEntity.getDtJson().size() >0 ){
                        selectList.add(biProWareEntity.getDtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getEtJson() != null && biProWareEntity.getEtJson().size() >0 ){
                        selectList.add(biProWareEntity.getEtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getEtJson() != null && biProWareEntity.getEtJson().size() >0 ){
                        selectList.add(biProWareEntity.getEtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    Integer limitNum = commonHelp.getChartLimit(biProWareEntity.getCorpid(), ChartLimitTypeEnum.BI_MUITI_DATA_NUM_LIMIT.getAlias());
                    updatePackageRedisService.chartLimit(selectList, biProWareEntity.getCorpid(),limitNum);
                } catch (XbbException e) {
                    LOG.error("===限制通不过====",e);
                    continue;
                }
                try {
                    updateBIData(biProWareEntity);
                } catch (Exception e) {
                    LOG.error("仓库更新出错"+biProWareEntity.getId(), e);
                }
            }
        }else {
            for (BiProWareEntity biProWareEntity : biProWareEntityList) {
                try {
                    if( chartLimitService.getCkFlag()){
                        LOG.error("当前公司数据源已经切换至CK=============="+biProWareEntity.getCorpid());
                        continue;
                    }
                    selectList = new ArrayList<>();
                    selectList.add(biProWareEntity.getAtJson().toJavaObject(SlaveSourcePojo.class));
                    if(biProWareEntity.getBtJson() != null && biProWareEntity.getBtJson().size() >0 ){
                        selectList.add(biProWareEntity.getBtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getCtJson() != null && biProWareEntity.getCtJson().size() >0 ){
                        selectList.add(biProWareEntity.getCtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getDtJson() != null && biProWareEntity.getDtJson().size() >0 ){
                        selectList.add(biProWareEntity.getDtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getEtJson() != null && biProWareEntity.getEtJson().size() >0 ){
                        selectList.add(biProWareEntity.getEtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    if(biProWareEntity.getEtJson() != null && biProWareEntity.getEtJson().size() >0 ){
                        selectList.add(biProWareEntity.getEtJson().toJavaObject(SlaveSourcePojo.class));
                    }
                    Integer limitNum = commonHelp.getChartLimit(biProWareEntity.getCorpid(), ChartLimitTypeEnum.BI_MUITI_DATA_NUM_LIMIT.getAlias());
                    updatePackageRedisService.chartLimit(selectList, biProWareEntity.getCorpid(),limitNum);
                } catch (XbbException e) {
                    LOG.error("===限制通不过====",e);
                    continue;
                }
                //TODO　上线使用线程  如果效率还有问题 考虑
                Runnable runnable = () -> {
                    try {
                        updateBIData(biProWareEntity);
                    } catch (Exception e) {
                        LOG.error("仓库更新出错"+biProWareEntity.getId(), e);
                    }
                };
                biUpdateThreadPool.execute(runnable);
            }
        }
        return dataInitVO;
    }

    @Override
    public DataInitVO updateBIDataByChartId(ReInitDataDTO reInitDataDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        String redisValue = paasRedisHelper.getValue(RedisPrefixConstant.BI_UPDATE_KEY+BiConstant.INDEX_SUFFIX+reInitDataDTO.getChartId(), reInitDataDTO.getXbbCode());
        if(!StringUtil.isEmpty(redisValue)){
            dataInitVO.setMessage("等着吧");
            //TODO  记得放开
            return  dataInitVO;
        }
        paasRedisHelper.setValue(RedisPrefixConstant.BI_UPDATE_KEY+BiConstant.INDEX_SUFFIX+reInitDataDTO.getChartId(),reInitDataDTO.getXbbCode(),reInitDataDTO.getXbbCode(), RedisConstant.ONE_MIN);
        LOG.info("=====updateStart========更新开始=========="+DateUtil.getInt());
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("id",reInitDataDTO.getChartId());
        List<ChartEntity> list = chartCustomModel.findEntitys(param);
        if(CollectionsUtil.isEmpty(list)){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
        }
        ChartEntity chartEntity = list.get(0);
        String  tableSerial = biProWareService.getTableSerial(chartEntity);
        List<BiProWareEntity> biProWareEntityList = biProWareService.getBiProWareEntities(chartEntity.getCorpid(),tableSerial);
        if(CollectionsUtil.isEmpty(biProWareEntityList)){
            dataInitVO.setMessage("仓库id为空");
            return dataInitVO;
        }
        List<SlaveSourcePojo> selectList = null;
        for (BiProWareEntity biProWareEntity : biProWareEntityList) {
            try {
                selectList = new ArrayList<>();
                selectList.add(biProWareEntity.getAtJson().toJavaObject(SlaveSourcePojo.class));
                if(biProWareEntity.getBtJson() != null && biProWareEntity.getBtJson().size() >0 ){
                    selectList.add(biProWareEntity.getBtJson().toJavaObject(SlaveSourcePojo.class));
                }
                if(biProWareEntity.getCtJson() != null && biProWareEntity.getCtJson().size() >0 ){
                    selectList.add(biProWareEntity.getCtJson().toJavaObject(SlaveSourcePojo.class));
                }
                if(biProWareEntity.getDtJson() != null && biProWareEntity.getDtJson().size() >0 ){
                    selectList.add(biProWareEntity.getDtJson().toJavaObject(SlaveSourcePojo.class));
                }
                if(biProWareEntity.getEtJson() != null && biProWareEntity.getEtJson().size() >0 ){
                    selectList.add(biProWareEntity.getEtJson().toJavaObject(SlaveSourcePojo.class));
                }
                if(biProWareEntity.getEtJson() != null && biProWareEntity.getEtJson().size() >0 ){
                    selectList.add(biProWareEntity.getEtJson().toJavaObject(SlaveSourcePojo.class));
                }
                Integer limitNum = commonHelp.getChartLimit(biProWareEntity.getCorpid(), ChartLimitTypeEnum.BI_MUITI_DATA_NUM_LIMIT.getAlias());
                updatePackageRedisService.chartLimit(selectList, biProWareEntity.getCorpid(),limitNum);
            } catch (XbbException e) {
                LOG.error("===限制通不过====",e);
                continue;
            }
            try {
                updateBIData(biProWareEntity);
            } catch (Exception e) {
                LOG.error("仓库更新出错"+biProWareEntity.getId(), e);
            }
        }
        LOG.info("=====updateStart========更新结束=========="+DateUtil.getInt());
        return dataInitVO;
    }

    private void updateBIData(BiProWareEntity biProWareEntity) throws Exception{
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("warehouseId", biProWareEntity.getId()));
        boolQueryBuilder.filter(termQuery(ParameterConstant.CORPID + StringConstant.POINT + StringConstant.KEY_WORD, biProWareEntity.getCorpid()));
        BiRelTableEnum biRelTableEnum = BiRelTableEnum.getByCode(biProWareEntity.getRelTable());
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,biRelTableEnum.getType()));
        sourceBuilder.query(boolQueryBuilder);
        sourceBuilder.fetchSource(new String[]{"updateTime","id","esId"}, null);
        //因为是只取一条，所以只有updateTime即可，不需要id二次排序
        sourceBuilder.sort("updateTime", SortOrder.DESC);
        SearchRequest searchRequest = new SearchRequest(biRelTableEnum.getIndex());
        searchRequest.source(sourceBuilder);
        //先查一条比updateTime
        PageRequest pageRequest = EsUtil.setPage(sourceBuilder, 1, 1);
        //分页
        XbbAggregatedPage<StatisticBiFormDataEntity> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, StatisticBiFormDataEntity.class);
        Long updateTime = 0L;
        if (esEntities == null || CollectionsUtil.isEmpty(esEntities.getContent())) {
           return;
        }else{
            StatisticBiFormDataEntity statisticBiFormDataEntity = esEntities.getContent().get(0);
            //TODO  拿最大更新时间会不会有问题  ？2020年5月7日10:45:59
            updateTime = statisticBiFormDataEntity.getUpdateTime();
        }
        JSONObject formIdAttr = biProWareEntity.getFormIdAttr();
        String corpid = biProWareEntity.getCorpid();
        updateData(biProWareEntity.getAtJson(), updateTime, formIdAttr, corpid, biRelTableEnum, FormLocationEnum.A.getCode(), biProWareEntity);
        updateData(biProWareEntity.getBtJson(), updateTime, formIdAttr, corpid, biRelTableEnum, FormLocationEnum.B.getCode(), biProWareEntity);
        updateData(biProWareEntity.getCtJson(), updateTime, formIdAttr, corpid, biRelTableEnum, FormLocationEnum.C.getCode(), biProWareEntity);
        updateData(biProWareEntity.getDtJson(), updateTime, formIdAttr, corpid, biRelTableEnum, FormLocationEnum.D.getCode(), biProWareEntity);
        updateData(biProWareEntity.getEtJson(), updateTime, formIdAttr, corpid, biRelTableEnum, FormLocationEnum.E.getCode(), biProWareEntity);
        updateData(biProWareEntity.getFtJson(), updateTime, formIdAttr, corpid, biRelTableEnum, FormLocationEnum.F.getCode(), biProWareEntity);
        //更新仓库的updateTime
        biProWareModel.updateTimeByKey(biProWareEntity.getId(),biProWareEntity.getCorpid());
    }

    /**
     * 数据的部分更新（只可用于更新操作）
     * @param atJson
     * @param updateTime
     * @param formIdAttr
     * @param corpid
     * @param biRelTableEnum
     * @param locationCode
     * @param biProWareEntity
     * @throws XbbException
     */
    private void updateData(JSONObject atJson, Long updateTime, JSONObject formIdAttr, String corpid,BiRelTableEnum biRelTableEnum, Integer locationCode,BiProWareEntity biProWareEntity ) throws Exception {
        if(atJson == null){
            return;
        }
        SlaveSourcePojo atJsonPojo =  atJson.toJavaObject(SlaveSourcePojo.class);
        PaasFormRefEntity paasFormRefEntity = null;
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("orderByStr", "update_time DESC ,id ASC");
        map.put("corpid", corpid);
        map.put("pageSize", SizeConstant.PAGE_SIZE);
        Long formId = atJsonPojo.getFormId();
        map.put("formId", formId);
        map.put("updateTimeStart", updateTime);
        if (BIProductReformEnum.getProductSubBusList().contains(atJson.getInteger("businessType"))) {
            paasFormRefEntity = paasFormRefModel.getMainFormIdByFormId(formId, corpid);
            if (Objects.isNull(paasFormRefEntity)) {
                throw new XbbException(BiErrorCodeEnum.API_ERROR_241022);
            }
            Long mainFormId = paasFormRefEntity.getMainFormId();
            map.put("formId",mainFormId);
        }
        boolean flag = true;
        List<PaasFormDataEntityExt> driverList;
        JSONObject kvJsonObject = (JSONObject) formIdAttr.get(formId.toString());
        /***==============================获取解释====================================***/
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Integer fieldType = atJsonPojo.getFieldType();
        Integer secondFieldType = atJsonPojo.getSecondFieldType();
        if(FieldTypeEnum.getExplainFieldTypeEnumType().contains(fieldType) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(secondFieldType) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(atJsonPojo.getLinkAttrCFieldType()) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(atJsonPojo.getLinkAttrDFieldType()) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(atJsonPojo.getLinkAttrEFieldType())){
            PaasFormEntityExt paasFormEntityExt = paasFormModel.getByKey(formId, corpid);
            if (paasFormEntityExt == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            //解释表信息
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(formId, corpid);
            if (paasFormExplainEntity == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
            List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
            if(!explainList.isEmpty()){
                if(SaasMarkEnum.SAAS.getCode().equals(atJsonPojo.getSaasMark())) {
                    for (FieldAttrEntity fieldAttrEntity : explainList) {
                        String attr = fieldAttrEntity.getAttr();
                        List<ItemPoJo> item = proFormHelp.getItem(fieldAttrEntity, atJsonPojo.getBusinessType(), corpid, atJsonPojo.getFormId());
                        fieldAttrEntity.setItems(item);
                        explainMap.put(attr, fieldAttrEntity);
                    }
                }else {
                    for (FieldAttrEntity fieldAttrEntity : explainList) {
                        String attr = fieldAttrEntity.getAttr();
                        explainMap.put(attr, fieldAttrEntity);
                    }
                }
            }
        }
        /***==============================获取解释====================================***/
        while (flag){
            driverList  = findEntitys(map, atJson.getInteger("businessType"), atJson.getInteger("saasMark"), paasFormRefEntity);
            if (driverList == null || driverList.isEmpty()) {
                flag = false;
            } else {
                map.put("idGte", driverList.get(driverList.size() - 1).getId());
                Map<Long, PaasFormDataEntityExt> updateMap = new HashMap(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                List<Long> formDataIdList = new ArrayList<>();
                for (PaasFormDataEntityExt dataEntityExt : driverList) {
                    Long id = dataEntityExt.getId();
                    if (Objects.isNull(dataEntityExt.getDataId())){
                        dataEntityExt.setDataId(id);
                    }
                    updateMap.put(id, dataEntityExt);
                    formDataIdList.add(id);
                }
                SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
                BoolQueryBuilder boolQueryBuilder = boolQuery();
                boolQueryBuilder.filter(termQuery("warehouseId", biProWareEntity.getId()));
                boolQueryBuilder.filter(termQuery(kvJsonObject.getString("formId"),map.get("formId")));
                boolQueryBuilder.filter(termsQuery(kvJsonObject.getString("dataId"), formDataIdList));
                boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,biRelTableEnum.getType()));
                sourceBuilder.query(boolQueryBuilder);
                SearchRequest searchRequest = new SearchRequest(biRelTableEnum.getIndex());
                searchRequest.source(sourceBuilder);
                EsUtil.setPage(sourceBuilder, 1, EsNormalConstant.FIND_NUM);
                //TODO  考虑改成queryForPage 因为上面限制了行数
                List<StatisticBiFormDataEntity> resultList =   xbbElasticsearchRestTemplate.findScrollList(StatisticBiFormDataEntity.class,searchRequest,SizeConstant.SCROLL_TIME_IN_MILLIS);
                if (resultList == null ) {
                    continue;
                }
                List<Long> incrDataIdList = new ArrayList<>();
                incrDataIdList .addAll((List<Long>) CloneUtil.deepClone(formDataIdList));
                Set<Long> updateIdSet = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                for (StatisticBiFormDataEntity statisticBiFormDataEntity : resultList) {
                    Object dataId = ReflectHelper.valueGet(statisticBiFormDataEntity, kvJsonObject.getString("dataId"), false);
                    if(dataId == null){
                        continue;
                    }
                    updateIdSet.add(Long.valueOf(dataId.toString()));
                }
                List<InsertDTO> updateList = new ArrayList<>();
                List<StatisticBiFormDataEntity> structureInsertList = new ArrayList<>(resultList.size());
                Map<Long,List<Long>>  slaveIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Long,Set<UserTeamEntity>> incrAUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                if( SaasMarkEnum.SAAS.getCode().equals(atJson.getInteger("saasMark"))){
                    BITeamEnum bITeamEnum = BITeamEnum.getByParentCode(atJson.getInteger("businessType"));
                    if(bITeamEnum !=null){
                        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(bITeamEnum.getCode());
                        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        params.put("orderByStr", "id");
                        params.put("dataIdIn", updateIdSet);
                        params.put("corpid", corpid);
                        boolean teamFlag = true;
                        while (teamFlag) {
                            List teamList = null;
                            teamList = teamHelp.getList(xbbRefTypeEnum, params, teamList);
                            if (teamList == null || teamList.isEmpty()) {
                                teamFlag = false;
                            } else {
                                //大于上次最大的id
                                teamHelp.setIdGte(xbbRefTypeEnum, params, teamList);
                                for (Object object : teamList) {
                                    UserTeamEntity userTeamEntity = (UserTeamEntity)object;
                                    Long dataId = userTeamEntity.getDataId();
                                    Set<UserTeamEntity> userTeamEntitySet;
                                    if(incrAUserMap.containsKey(dataId)){
                                        userTeamEntitySet = incrAUserMap.get(dataId);
                                        if(userTeamEntitySet == null){
                                            userTeamEntitySet = new HashSet<>();
                                        }

                                    }else {
                                        userTeamEntitySet = new HashSet<>();
                                    }
                                    userTeamEntitySet.add(userTeamEntity);
                                    incrAUserMap.put(dataId,userTeamEntitySet);
                                }
                            }
                        }
                    }
                }
                for (StatisticBiFormDataEntity statisticBiFormDataEntity : resultList) {
                    Object dataId = ReflectHelper.valueGet(statisticBiFormDataEntity, kvJsonObject.getString("dataId"), false);
                    PaasFormDataEntityExt formDataEntityExt = updateMap.get(dataId);
                    if(formDataEntityExt == null){
                        continue;
                    }
                    long now = DateUtil.getNow();
                    statisticBiFormDataEntity.setUpdateTime(now);
//                    JSONObject statisticBiJson = JSONObject.parseObject(JSON.toJSONString(statisticBiFormDataEntity,SerializerFeature.DisableCircularReferenceDetect));
                    //此处使用new出来的载体，使用doc跟新，防止幻读
                    JSONObject statisticBiJson = new JSONObject();
                    integrateStasticBiFormData(formIdAttr, statisticBiJson, formDataEntityExt,atJsonPojo);
                    incrDataIdList.remove(Long.valueOf(dataId.toString()));
                    statisticBiJson.put(ElasticConstant.MY_JOIN_FIELD, biRelTableEnum.getType());
                    statisticBiJson.put("parent", statisticBiFormDataEntity.getEsId());
                    structUserTeam(statisticBiJson,  incrAUserMap.get(dataId),locationCode);
                    StatisticBiFormDataEntity newEntity = statisticBiJson.toJavaObject(StatisticBiFormDataEntity.class);
                    newEntity.setMy_join_field(biRelTableEnum.getType());
                    newEntity.setEsId(statisticBiFormDataEntity.getEsId());
                    newEntity.setParent(statisticBiFormDataEntity.getEsId());
                    setSlaveIdMap(slaveIdMap, statisticBiFormDataEntity.getDataId_1(), Long.valueOf(dataId.toString()));
                    newEntity.setUpdateTime(now);
                    structureInsertList.add(newEntity);
                    String dir = ProBaseConfig.getProjectName();
                    if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                        InsertDTO insertDTO = new InsertDTO();
                        insertDTO.setEsId(statisticBiFormDataEntity.getEsId());
                        insertDTO.setSource(statisticBiJson);
                        insertDTO.setIndex(biRelTableEnum.getIndex());
                        insertDTO.setType(biRelTableEnum.getType());
                        insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
                        updateList.add(insertDTO);
                    }
                }
                resultList.clear();
                String dir = ProBaseConfig.getProjectName();
                IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid, atJson.getInteger("businessType"), atJson.getInteger("saasMark"));
                if(CollectionsUtil.isNotEmpty(updateList) || CollectionsUtil.isNotEmpty(structureInsertList)){
                    if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                        paasEsModel.updateBatch(updateList,WriteRequest.RefreshPolicy.NONE,false);
                    }else {
                        initMessageQueueHelp.initBiMessage(structureInsertList, biRelTableEnum.getIndex() + ChartConstant.UPDATE_SUFFIX);
                        structureInsertList.clear();
                    }
                    //子表单的更新  //TODO  更新时间要传入
                    structureSubData(corpid, indexTypeEnum, biRelTableEnum, biProWareEntity.getId(), slaveIdMap.keySet(), formIdAttr, slaveIdMap);
                    slaveIdMap.clear();
                }
                //增量的部分
                if(Objects.equals(FormLocationEnum.A.getCode(),locationCode)){
                    List<PaasFormDataEntityExt>  incrList = new ArrayList<>();
                    Set<Long> dataIdSet = new HashSet<>();
                    for (PaasFormDataEntityExt paasFormDataEntityExt : driverList) {
                        if(incrDataIdList.contains(paasFormDataEntityExt.getDataId())){
                            incrList.add(paasFormDataEntityExt);
                            dataIdSet.add(paasFormDataEntityExt.getId());
                        }
                    }
                    if(CollectionsUtil.isEmpty(incrList)){
                        continue;
                    }
                    List<SlaveSourcePojo> slaveSourceListForCorrelation = new ArrayList<>();
                    JSONObject btJson = biProWareEntity.getBtJson();
                    IndexTypeEnum indexTypeEnumB = null;
                    if(btJson !=null){
                        slaveSourceListForCorrelation.add(btJson.toJavaObject(SlaveSourcePojo.class));
                        indexTypeEnumB = indexTypeModel.getIndexType(corpid, btJson.getInteger("businessType"), btJson.getInteger("saasMark"));

                    }
                    IndexTypeEnum indexTypeEnumC = null;
                    IndexTypeEnum indexTypeEnumD = null;
                    IndexTypeEnum indexTypeEnumE = null;
                    IndexTypeEnum indexTypeEnumF = null;
                    JSONObject ctJson = biProWareEntity.getCtJson();
                    if(ctJson != null){
                        slaveSourceListForCorrelation.add(ctJson.toJavaObject(SlaveSourcePojo.class));
//                        indexTypeEnumC = EsHelper.getIndexTypeEnum(ctJson.getInteger("saasMark"),ctJson.getInteger("businessType"));
                        indexTypeEnumC = indexTypeModel.getIndexType(corpid, ctJson.getInteger("businessType"), ctJson.getInteger("saasMark"));
                    }
                    JSONObject dtJson = biProWareEntity.getDtJson();
                    if(dtJson != null){
                        slaveSourceListForCorrelation.add(dtJson.toJavaObject(SlaveSourcePojo.class));
//                        indexTypeEnumD = EsHelper.getIndexTypeEnum(dtJson.getInteger("saasMark"),dtJson.getInteger("businessType"));
                        indexTypeEnumD = indexTypeModel.getIndexType(corpid, dtJson.getInteger("businessType"), dtJson.getInteger("saasMark"));
                    }
                    JSONObject etJson = biProWareEntity.getEtJson();
                    if(etJson != null){
                        slaveSourceListForCorrelation.add(etJson.toJavaObject(SlaveSourcePojo.class));
//                        indexTypeEnumE = EsHelper.getIndexTypeEnum(etJson.getInteger("saasMark"),etJson.getInteger("businessType"));
                        indexTypeEnumE = indexTypeModel.getIndexType(corpid, etJson.getInteger("businessType"), etJson.getInteger("saasMark"));
                    }
                    JSONObject ftJson = biProWareEntity.getFtJson();
                    if(ftJson != null){
                        slaveSourceListForCorrelation.add(ftJson.toJavaObject(SlaveSourcePojo.class));
//                        indexTypeEnumF = EsHelper.getIndexTypeEnum(ftJson.getInteger("saasMark"),ftJson.getInteger("businessType"));
                        indexTypeEnumF = indexTypeModel.getIndexType(corpid, ftJson.getInteger("businessType"), ftJson.getInteger("saasMark"));
                    }
                    Set<Object> linkBFilterSet = new HashSet<>();
                    Set<Object> linkCFilterSet = new HashSet<>();
                    Set<Object> linkDFilterSet = new HashSet<>();
                    Set<Object> linkEFilterSet = new HashSet<>();
                    Set<Object> linkFFilterSet = new HashSet<>();
                    for (PaasFormDataEntityExt paasFormDataEntityExt : incrList) {
                        if(atJsonPojo.getLinkAttrCopy() != null){
                            setAFilterSet(atJsonPojo.getInData(), explainMap, atJsonPojo.getFieldType(), linkBFilterSet, paasFormDataEntityExt, atJsonPojo.getLinkAttrCopy());
                        }
                        if(indexTypeEnumC != null){
                            setAFilterSet(atJsonPojo.getSecondInData(), explainMap, atJsonPojo.getSecondFieldType(), linkCFilterSet, paasFormDataEntityExt, atJsonPojo.getLinkAttrBCopy());
                        }
                        if(indexTypeEnumD != null){
                            setAFilterSet(atJsonPojo.getLinkAttrCInData(), explainMap, atJsonPojo.getLinkAttrCFieldType(), linkDFilterSet, paasFormDataEntityExt, atJsonPojo.getLinkAttrCCopy());
                        }
                        if(indexTypeEnumE != null){
                            setAFilterSet(atJsonPojo.getLinkAttrDInData(), explainMap, atJsonPojo.getLinkAttrDFieldType(), linkEFilterSet, paasFormDataEntityExt, atJsonPojo.getLinkAttrDCopy());
                        }
                        if(indexTypeEnumF != null){
                            setAFilterSet(atJsonPojo.getLinkAttrEInData(), explainMap, atJsonPojo.getLinkAttrEFieldType(), linkFFilterSet, paasFormDataEntityExt, atJsonPojo.getLinkAttrECopy());
                        }
                    }
                    Map<Object,PaasFormDataEntityExt> bMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Object,PaasFormDataEntityExt> cMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Object,PaasFormDataEntityExt> dMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Object,PaasFormDataEntityExt> eMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Object,PaasFormDataEntityExt> fMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    //团队的处理
                    Map<Long,Set<UserTeamEntity>> aUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Long,Set<UserTeamEntity>> bUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Long,Set<UserTeamEntity>> cUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Long,Set<UserTeamEntity>> dUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Long,Set<UserTeamEntity>> eUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Long,Set<UserTeamEntity>> fUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    //团队数据的处理
                    if( SaasMarkEnum.SAAS.getCode().equals(atJson.getInteger("saasMark"))){
                        BITeamEnum bITeamEnum = BITeamEnum.getByParentCode(atJson.getInteger("businessType"));
                        if(bITeamEnum !=null){
                            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(bITeamEnum.getCode());
                            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            params.put("orderByStr", "id");
                            params.put("dataIdIn", dataIdSet);
                            params.put("corpid", corpid);
                            boolean teamFlag = true;
                            while (teamFlag) {
                                List teamList = null;
                                teamList = teamHelp.getList(xbbRefTypeEnum, params, teamList);
                                if (teamList == null || teamList.isEmpty()) {
                                    teamFlag = false;
                                } else {
                                    //大于上次最大的id
                                    teamHelp.setIdGte(xbbRefTypeEnum, params, teamList);
                                    for (Object object : teamList) {
                                        UserTeamEntity userTeamEntity = (UserTeamEntity)object;
                                        Long dataId = userTeamEntity.getDataId();
                                        Set<UserTeamEntity> userTeamEntitySet;
                                        if(aUserMap.containsKey(dataId)){
                                            userTeamEntitySet = aUserMap.get(dataId);
                                            if(userTeamEntitySet == null){
                                                userTeamEntitySet = new HashSet<>();
                                            }

                                        }else {
                                            userTeamEntitySet = new HashSet<>();
                                        }
                                        userTeamEntitySet.add(userTeamEntity);
                                        aUserMap.put(dataId,userTeamEntitySet);
                                    }
                                }
                            }
                        }
                    }
                    //第一张从表
                    if(indexTypeEnumB != null){
                        SlaveListDTO slaveListDTO = new SlaveListDTO();
                        BeanUtils.copyProperties(btJson.toJavaObject(SlaveSourcePojo.class),slaveListDTO);
                        slaveListDTO.setCorpid(corpid);
                        slaveListDTO.setSlaveSet(linkBFilterSet);
                        slaveListDTO.setSlavesMap(bMap);
                        slaveListDTO.setSlavesUserMap(bUserMap);
                        slaveListDTO.setIndexTypeEnum(indexTypeEnumB);
                        slaveListDTO.setWarehouseId(biProWareEntity.getId());
                        slaveListDTO.setBiRelTableEnum(biRelTableEnum);
                        slaveListDTO.setDriverBusinessType(atJson.getInteger("businessType"));
                        slaveList(slaveListDTO);
                    }
                    //第二张从表
                    if(indexTypeEnumC != null){
                        SlaveListDTO secondSlaveListDTO = new SlaveListDTO();
                        BeanUtils.copyProperties(ctJson.toJavaObject(SlaveSourcePojo.class),secondSlaveListDTO);
                        secondSlaveListDTO.setCorpid(corpid);
                        secondSlaveListDTO.setSlaveSet(linkCFilterSet);
                        secondSlaveListDTO.setSlavesMap(cMap);
                        secondSlaveListDTO.setSlavesUserMap(cUserMap);
                        secondSlaveListDTO.setIndexTypeEnum(indexTypeEnumC);
                        secondSlaveListDTO.setWarehouseId(biProWareEntity.getId());
                        secondSlaveListDTO.setBiRelTableEnum(biRelTableEnum);
                        //获取从表数据
                        slaveList(secondSlaveListDTO);
                    }
                    //第3张从表
                    if(indexTypeEnumD != null){
                        SlaveListDTO secondSlaveListDTO = new SlaveListDTO();
                        BeanUtils.copyProperties(dtJson.toJavaObject(SlaveSourcePojo.class),secondSlaveListDTO);
                        secondSlaveListDTO.setCorpid(corpid);
                        secondSlaveListDTO.setSlaveSet(linkDFilterSet);
                        secondSlaveListDTO.setSlavesMap(dMap);
                        secondSlaveListDTO.setSlavesUserMap(dUserMap);
                        secondSlaveListDTO.setIndexTypeEnum(indexTypeEnumD);
                        secondSlaveListDTO.setWarehouseId(biProWareEntity.getId());
                        secondSlaveListDTO.setBiRelTableEnum(biRelTableEnum);
                        //获取从表数据
                        slaveList(secondSlaveListDTO);
                    }
                    //第4张从表
                    if(indexTypeEnumE != null){
                        SlaveListDTO secondSlaveListDTO = new SlaveListDTO();
                        BeanUtils.copyProperties(etJson.toJavaObject(SlaveSourcePojo.class),secondSlaveListDTO);
                        secondSlaveListDTO.setCorpid(corpid);
                        secondSlaveListDTO.setSlaveSet(linkEFilterSet);
                        secondSlaveListDTO.setSlavesMap(eMap);
                        secondSlaveListDTO.setSlavesUserMap(eUserMap);
                        secondSlaveListDTO.setIndexTypeEnum(indexTypeEnumE);
                        secondSlaveListDTO.setWarehouseId(biProWareEntity.getId());
                        secondSlaveListDTO.setBiRelTableEnum(biRelTableEnum);
                        //获取从表数据
                        slaveList(secondSlaveListDTO);
                    }
                    //第5张从表
                    if(indexTypeEnumF != null){
                        SlaveListDTO secondSlaveListDTO = new SlaveListDTO();
                        BeanUtils.copyProperties(btJson.toJavaObject(SlaveSourcePojo.class),secondSlaveListDTO);
                        secondSlaveListDTO.setCorpid(corpid);
                        secondSlaveListDTO.setSlaveSet(linkFFilterSet);
                        secondSlaveListDTO.setSlavesMap(fMap);
                        secondSlaveListDTO.setSlavesUserMap(fUserMap);
                        secondSlaveListDTO.setIndexTypeEnum(indexTypeEnumF);
                        secondSlaveListDTO.setWarehouseId(biProWareEntity.getId());
                        secondSlaveListDTO.setBiRelTableEnum(biRelTableEnum);
                        //获取从表数据
                        slaveList(secondSlaveListDTO);
                    }
                    //拼装数据
                    List<InsertDTO> insertList = new ArrayList<>();
                    StructureBiInsertDTO structureBiInsertDTO = new StructureBiInsertDTO(corpid, indexTypeEnumC != null, atJsonPojo.getLinkAttrCopy(), biRelTableEnum, slaveSourceListForCorrelation, biProWareEntity.getId(),
                             driverList, atJsonPojo.getInData(),bMap, cMap, dMap, eMap, fMap,  insertList, explainMap, fieldType, atJsonPojo.getLinkAttrB(), secondFieldType, atJsonPojo.getSecondInData(), biProWareEntity.getFormIdAttr(),
                            atJsonPojo.getLinkAttrC(),atJsonPojo.getLinkAttrCInData(),atJsonPojo.getLinkAttrCFieldType(),
                            atJsonPojo.getLinkAttrD(),atJsonPojo.getLinkAttrDInData(),atJsonPojo.getLinkAttrDFieldType(),
                            atJsonPojo.getLinkAttrE(),atJsonPojo.getLinkAttrEInData(),atJsonPojo.getLinkAttrEFieldType(),atJsonPojo.getBusinessType(),atJsonPojo.getFormId()
                    );
                    structureBiInsertDTO.setAUserMap(aUserMap);
                    structureBiInsertDTO.setBUserMap(bUserMap);
                    structureBiInsertDTO.setCUserMap(cUserMap);
                    structureBiInsertDTO.setDUserMap(dUserMap);
                    structureBiInsertDTO.setEUserMap(eUserMap);
                    structureBiInsertDTO.setFUserMap(fUserMap);
                    List<StatisticBiFormDataEntity> structureInsertListNew = structureBiInsertList(structureBiInsertDTO);
                    // todo 测试二缺少自定义统计集群环境，暂时用本地的 || Objects.equals(dir, ProVersionEnum.TEST2.getAlias())
                    if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                        paasEsModel.insertBatch(insertList,WriteRequest.RefreshPolicy.NONE,false);
                    }else {
                        initMessageQueueHelp.initBiMessage(structureInsertListNew, biRelTableEnum.getIndex());
                        structureInsertListNew.clear();
                    }
                    //子表单数据的处理
                    Long biProWareEntityId = biProWareEntity.getId();
                    structureSubData(corpid, indexTypeEnum, biRelTableEnum, biProWareEntityId, dataIdSet, formIdAttr,null);
                    if (indexTypeEnumB != null) {
                        structureSubData(corpid, indexTypeEnumB, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveBIdMap().keySet(), formIdAttr, structureBiInsertDTO.getSlaveBIdMap());
                    }
                    if (indexTypeEnumC != null) {
                        structureSubData(corpid, indexTypeEnumC, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveCIdMap().keySet(), formIdAttr, structureBiInsertDTO.getSlaveCIdMap());
                    }
                    if (indexTypeEnumD != null) {
                        structureSubData(corpid, indexTypeEnumD, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveDIdMap().keySet(), formIdAttr, structureBiInsertDTO.getSlaveDIdMap());
                    }
                    if (indexTypeEnumE != null) {
                        structureSubData(corpid, indexTypeEnumE, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveEIdMap().keySet(), formIdAttr, structureBiInsertDTO.getSlaveEIdMap());
                    }
                    if (indexTypeEnumF != null) {
                        structureSubData(corpid, indexTypeEnumF, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveFIdMap().keySet(), formIdAttr, structureBiInsertDTO.getSlaveFIdMap());
                    }
                    //手动清除内层集合的数据
                    cleanS(structureBiInsertDTO);
                }
            }
        }
    }




    @Override
    public DataInitVO deleteAllData(MappingInitDTO mappingInitDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        try {
            for (BiRelTableEnum biRelTableEnum : BiRelTableEnum.values()) {
                try {
                    deleteByQuery(biRelTableEnum);
                } catch (Exception e) {
                    LOG.error("删除失败的索引================"+biRelTableEnum);
                    //TODO 已存在则跳过
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            throw  new XbbException(BiErrorCodeEnum.API_ERROR_241011);
        }
        return dataInitVO;
    }

    @Override
    public DataInitVO initLogStash(LogStashEditDTO logStashEditDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("input {").append("\n");
        strBuilder.append("\tstdin {").append("\n").append("\t}").append("\n");
        LogStashInputPojo logStashInputPojo = new LogStashInputPojo();
        logStashInputPojo.setHost("${RABBIT_MQ}");
        logStashInputPojo.setPort(logStashEditDTO.getPort());
        logStashInputPojo.setUser(logStashEditDTO.getUser());
        logStashInputPojo.setPassword(logStashEditDTO.getPassword());
        //当前版本环境 开发/测试/本地
        String version = logStashEditDTO.getVersion();
        String path = "D:/mq_logstash_pb_v1.conf";
        String exchange = "test-bi";
        String virtualHost = "/playback-bi";
        String esName = "prd_";
        String localEsName = ProBaseConfig.getEsName();
        //开发
        if (Objects.equals(version, ProVersionEnum.DEV.getAlias())){
            exchange = "bi";
            virtualHost = "/bi";
            path = "D:/mq_logstash_dev.conf";
            esName = "dev_";
        }
        //测试
        else if(Objects.equals(version, ProVersionEnum.TEST.getAlias())){
            exchange = "test-bi";
            virtualHost = "/test-bi";
            path = "D:/mq_logstash_test1_v1.conf";
            esName = "test_";
        }
        else if(Objects.equals(version, ProVersionEnum.TEST2.getAlias())){
            exchange = "test-bi";
            virtualHost = "/test-bi2";
            path = "D:/mq_logstash_test2_v1.conf";
            esName = "test2_";
        }
        else if(Objects.equals(version, ProVersionEnum.LOCAL.getAlias())){
            exchange = "bi";
            virtualHost = "/bi";
            path = "D:/mq_logstash.conf";
            esName = "es7_";
        }
        else if(Objects.equals(version, ProVersionEnum.PRD.getAlias())){
            exchange = "bi";
            virtualHost = "/bi";
            path = "D:/mq_logstash_prd.conf";
            esName = "prd_";
        }
        logStashInputPojo.setExchange(exchange);
        logStashInputPojo.setDurable(true);
        logStashInputPojo.setThreads(2);
        logStashInputPojo.setVirtualHost(virtualHost);

        for(BiRelTableEnum biRelTableEnum : BiRelTableEnum.values()) {
            String index = biRelTableEnum.getIndex();
            String indexClean = index.replace(BiConstant.INDEX_SUFFIX,"");
            indexClean =  indexClean.replace(localEsName,"");
            logStashInputPojo.setQueue(esName+indexClean);
            logStashInputPojo.setType(esName+indexClean+BiConstant.INDEX_SUFFIX);
            logStashInputPojo.setKey(indexClean);
            strBuilder.append(logStashInputPojo.getLogStashInput()).append("\n");
            strBuilder.append(logStashInputPojo.getLogStashInputUpdate()).append("\n");
        }
        strBuilder.append("}").append("\n");
        strBuilder.append("output {").append("\n");
        strBuilder.append("\tstdout {").append("\n").append("\t}").append("\n");
        LogStashOutputPojo logStashOutputPojo = new LogStashOutputPojo();
        //es IP地址端口号
        logStashOutputPojo.setHosts(logStashEditDTO.getHosts());
        logStashOutputPojo.setUser("${ELASTIC_NAME}");
        logStashOutputPojo.setPassword("${ELASTIC_PASSWORD}");
        for(BiRelTableEnum biRelTableEnum : BiRelTableEnum.values()) {
            String index = biRelTableEnum.getIndex();
            index = index.replaceAll(localEsName,esName);
            logStashOutputPojo.setIndex(index);
//            logStashOutputPojo.setDocumentType(biRelTableEnum.getType());
            strBuilder.append(logStashOutputPojo.getLogStashOutput()).append("\n");
            strBuilder.append(logStashOutputPojo.getLogStashOutputUpdate()).append("\n");
        }
        strBuilder.append("}");
        FileOutputStream fos = null;
        PrintWriter pw = null;
        OutputStreamWriter oStreamWriter = null;
        try {
            File dir = new File(path);
            boolean isCreate = false;
            if (dir.exists()) {
                boolean isDelete = dir.delete();
                if (isDelete) {
                    isCreate = dir.createNewFile();
                }
            } else {
                isCreate = dir.createNewFile();
            }
            if (isCreate) {
                fos = new FileOutputStream(dir);
                oStreamWriter = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
                pw = new PrintWriter(oStreamWriter);
                pw.write(strBuilder.toString().toCharArray());
                pw.flush();
            }
        }catch (Exception e){
            LOG.error("生产logStash配置文件报错",e);
        }finally {
            try {
                if(oStreamWriter != null) {
                    oStreamWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(pw != null) {
                pw.close();
            }
        }
        return dataInitVO;
    }

    @Override
    public DataInitVO initAllData(InitAllDTO initAllDTO) throws XbbException {
        DataInitVO dataInitVO = new DataInitVO();
        ChartEditDTO chartEditDTO = new ChartEditDTO();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del",0);
        //muti=0 是多表  包含多源  不再以slaveSouse为判断依据
        param.put("muti", 0);
        param.put("columns", "corpid,id,slave_sources");
        param.put("orderByStr", "id");
        param.put("pageSize", SizeConstant.PAGE_SIZE);
        Long updateTimeStart = initAllDTO.getUpdateTimeStart();
        if(updateTimeStart != null && updateTimeStart>0){
            param.put("updateTimeStart", initAllDTO.getUpdateTimeStart());
        }
        Long idGte = initAllDTO.getIdGte();
        if(idGte != null && idGte>0){
            param.put("idGte", idGte);
        }
        if(CollectionsUtil.isNotEmpty(initAllDTO.getCorpidList())){
            param.put("corpidIn", initAllDTO.getCorpidList());
        }
        LOG.error("主线程开始========"+DateUtil.getInt());
        boolean flag = true;
        while (flag){
            List<ChartEntity>  list = chartCustomModel.findEntitys(param);
            LOG.error("reIndexsize()========"+ list.size());
            if(list == null || list.isEmpty()){
                flag = false;
            }else {
                param.put("idGte",list.get(list.size() - 1).getId());
                for (ChartEntity chartEntity : list) {
                    try {
                        chartEditDTO.setChartId(chartEntity.getId());
                        chartEditDTO.setCorpid(chartEntity.getCorpid());
                        chartEditDTO.setStartRun(true);
                        LOG.info("chartEntity.getId()========"+chartEntity.getId());
                        initData(chartEditDTO);
                        LOG.info("chartEntity.getId()====success===="+chartEntity.getId());
                    } catch (Exception e) {
                        LOG.error("",e);
                    }
                }
            }
        }
        LOG.error("主线程全部success========"+DateUtil.getInt());
        return dataInitVO;
    }

    @Deprecated
    private void deleteByQuery(BiRelTableEnum biRelTableEnum) throws IOException {
        //TODO  2019年7月1日17:34:57    是否要做scrooll
        HttpEntity httpEntity = null;
        try {
            /*httpEntity = new NStringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON);
            Response response = xbbElasticsearchRestTemplate.getRestClient(index).performRequest("POST", index + "/" + biRelTableEnum.getType() + "/_delete_by_query", Collections.emptyMap(), httpEntity);*/
            String index = biRelTableEnum.getIndex();
            DeleteByQueryRequest request = new DeleteByQueryRequest(index);
            request.setConflicts("proceed");
            xbbElasticsearchRestTemplate.getRestHighLevelClient(index).deleteByQuery(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            LOG.error("",e);
        } finally {
            if (Objects.nonNull(httpEntity)) {
                try {
                    ((NStringEntity) httpEntity).close();
                } catch (Exception e) {
                    LOG.error("dataWarehouse.deleteByQuery",e);
                }
            }
        }
    }

    private void initBiMapping(BiRelTableEnum biRelTableEnum) throws XbbException {
        HttpEntity httpEntity = null;
        try {
            String indexName = biRelTableEnum.getIndex() ;
            JSONObject rootSetting = (JSONObject) JSONObject.parse(EsMappingConstant.ELASTIC_STATISTIC_SETTINGS_ROOT);
            //----------------------上面是创建index  ，下面是mapping

            JSONObject mappingJson = (JSONObject) JSONObject.parse(EsMappingConstant.ELASTIC_MAPPING_ROOT);
            JSONObject properties = mappingJson.getJSONObject("properties");
            //外层
            JSONObject outJsonObject = new JSONObject();
            //内层
            JSONObject subFormJson = new JSONObject();
            //公有部分
            JSONObject newJsonObject = new JSONObject();
            //内层数量减半
            JSONObject inJsonObject = new JSONObject();

            for (int i = 1; i <= BiEsAliasEnum.TEXT.getMax(); i++) {
                newJsonObject.put("text_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_STRING_SORT));
            }
            for (int i = 1; i <= BiEsAliasEnum.NUM.getMax(); i++) {
                newJsonObject.put("num_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DOUBLE));
            }
            for (int i = 1; i <= BiEsAliasEnum.DATE.getMax(); i++) {
                newJsonObject.put("date_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DATE_SECOND));
            }
            for (int i = 1; i <= BiEsAliasEnum.LOCATION.getMax(); i++) {
                newJsonObject.put("location_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_LOCATION));
            }
            for (int i = 1; i <= BiEsAliasEnum.FILE.getMax(); i++) {
                newJsonObject.put("file_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_NOT_ANALYZED));
            }
            //数组类型 内部定义为
            for (int i = 1; i <= BiEsAliasEnum.ARRAY.getMax(); i++) {
                newJsonObject.put("array_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_TEXT));
            }
            //子表单
            for (int i = 1; i <= EsAliasEnum.PHONE_TEXT.getMax(); i++) {
                inJsonObject.put("text_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_STRING_SORT));
            }
            for (int i = 1; i <= EsAliasEnum.PHONE_NUM.getMax(); i++) {
                inJsonObject.put("num_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DOUBLE));
            }
            subFormJson.put("properties", inJsonObject);
            //子表单数量限制3个
            //TODO
            for (int i = 1; i <= EsAliasEnum.SUB_FORM.getMax(); i++) {
                outJsonObject.put("subForm_" + i, subFormJson);
            }
            //
            for (int i = 1; i <= EsAliasEnum.MY_TEAM.getMax(); i++) {
                outJsonObject.put("myTeam_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_NESTED));
            }
            //addTime
            outJsonObject.put("addTime", (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DATE_SECOND));
            //lastConnectTime
            outJsonObject.put("updateTime", (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DATE_SECOND));
            //序列号
            outJsonObject.put("serialNo", (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_STRING_SORT));
            String subRelate = "{\"type\":\"join\",\"relations\":{\"" + biRelTableEnum.getType()+ "\":" + "[\"" + ElasticConstant.SUB_FORM_TYPE+ "\"]}}";
            outJsonObject.put(ElasticConstant.MY_JOIN_FIELD, (JSONObject) JSONObject.parse(subRelate));

            outJsonObject.putAll(newJsonObject);
            properties.putAll(outJsonObject);
            JSONObject rootJson = new JSONObject();
            rootJson.put("mappings", mappingJson);
            rootJson.put("settings", rootSetting);
            LOG.info("settingAndMapping============="+JSON.toJSONString(rootJson, SerializerFeature.DisableCircularReferenceDetect));
            CreateIndexRequest request = new CreateIndexRequest(indexName);
            request.source(rootJson);
            CreateIndexResponse createIndexResponse = xbbElasticsearchRestTemplate.getRestHighLevelClient(indexName).indices().create(request, RequestOptions.DEFAULT);
            boolean acknowledged = createIndexResponse.isAcknowledged();
            if(acknowledged !=true){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
            }
        } catch (Exception e) {
            LOG.error("mapping创建失败", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }finally {
            if (Objects.nonNull(httpEntity)) {
                try {
                    ((NStringEntity) httpEntity).close();
                } catch (Exception e) {
                    LOG.error("mapping创建失败",e);
                }
            }
        }
    }
    /**
     * 更新数据
     * @param updateIdList
     * @param updateMap
     * @param formId
     * @param locationCode
     * @param formLocationEnum
     * @param biRelTableEnum
     * @throws XbbException
     */
    private List<StatisticPaasFormDataEsEntity> updateList(List<Long> updateIdList, Map<Long, PaasFormDataEntityExt> updateMap, Long formId, Integer locationCode, FormLocationEnum formLocationEnum, BiRelTableEnum biRelTableEnum) throws XbbException {
        String location = formLocationEnum.getLocation();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery( location+"formId",formId));
        boolQueryBuilder.filter(termsQuery(location +"dataId", updateIdList));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,biRelTableEnum.getType()));
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(biRelTableEnum.getIndex());
        searchRequest.source(sourceBuilder);
        EsUtil.setPage(sourceBuilder, 1, EsNormalConstant.FIND_NUM);
        List<StatisticPaasFormDataEsEntity> resultList =   xbbElasticsearchRestTemplate.findScrollList(StatisticPaasFormDataEsEntity.class,searchRequest,SizeConstant.SCROLL_TIME_IN_MILLIS);
        if (resultList == null) {
            throw new XbbException(BiErrorCodeEnum.API_ERROR_241009);
        }
        if(resultList.size() > 0){
            Long updateKey = null;
            List<InsertDTO> insertList = new ArrayList<>();
            for (StatisticPaasFormDataEsEntity statisticPaasFormDataEsEntity : resultList) {
                switch (formLocationEnum) {
                    case A:
                        updateKey = statisticPaasFormDataEsEntity.getAtdataId();
                        break;
                    case B:
                        updateKey = statisticPaasFormDataEsEntity.getBtdataId();
                        break;
                    case C:
                        updateKey = statisticPaasFormDataEsEntity.getCtdataId();
                        break;
                    default:
                        break;
                }
                if(updateKey == null){
                    //TODO  是不是应该抛出
                    continue;
                }
                PaasFormDataEntityExt formDataEntityExt = updateMap.get(updateKey);
                if(formDataEntityExt == null){
                    continue;
                }
                switch (formLocationEnum) {
                    case A:
                        statisticPaasFormDataEsEntity.putAtEntity(formDataEntityExt);
                        break;
                    case B:
                        statisticPaasFormDataEsEntity.putBtEntity(formDataEntityExt);
                        break;
                    case C:
                        statisticPaasFormDataEsEntity.putCtEntity(formDataEntityExt);
                        break;
                    default:
                        break;
                }
                InsertDTO insertDTO = new InsertDTO();
                insertDTO.setEsId(statisticPaasFormDataEsEntity.getEsId());
                Map<String, Object> insertMap = BeanUtil.convertBean2Map(statisticPaasFormDataEsEntity,false,true);
                insertDTO.setSource(insertMap);
                insertDTO.setIndex(biRelTableEnum.getIndex());
                insertDTO.setType(biRelTableEnum.getType());
                insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
                insertList.add(insertDTO);
            }
            //TODO 后面改成消息
            String dir = ProBaseConfig.getProjectName();
            if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                paasEsModel.insertBatch(insertList);
            }else {
                initMessageQueueHelp.initMessage(resultList, biRelTableEnum.getIndex() + ChartConstant.UPDATE_SUFFIX);
            }
            return resultList;
        }
        return  new ArrayList<>();
    }

    /**
     * 团队的更新
     * @param businessType
     * @param saasMark
     * @param formLocationEnum
     * @param biRelTableEnum
     * @param updatedList
     * @throws XbbException
     */
    private void updateTeam(Integer businessType, Integer saasMark, FormLocationEnum formLocationEnum, BiRelTableEnum biRelTableEnum, List<StatisticPaasFormDataEsEntity> updatedList) throws XbbException {
        if(CollectionsUtil.isNotEmpty(updatedList)){
            Map<Long, Set<String>> teamLinkMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (StatisticPaasFormDataEsEntity statisticPaasFormDataEsEntity : updatedList) {
                String esId = statisticPaasFormDataEsEntity.getEsId();
                Long dataId = null;
                switch (formLocationEnum) {
                    case A:
                        dataId = statisticPaasFormDataEsEntity.getAtdataId();
                        break;
                    case B:
                        dataId = statisticPaasFormDataEsEntity.getBtdataId();
                        break;
                    case C:
                        dataId = statisticPaasFormDataEsEntity.getCtdataId();
                        break;
                    default:
                        break;
                }
                setTeamLinkMap(teamLinkMap, esId, dataId);
            }
            initTeamData(businessType, saasMark, biRelTableEnum, updatedList.get(0).getWarehouseId(), teamLinkMap, formLocationEnum);
        }
    }

    /**
     * 根据条件查询出列表数据
     *
     * @param map 查询条件map
     * @param businessType 业务类型
     * @param saasMark
     * @param paasFormRefEntity 表单关系表，主要用于产品子表单那边需要。并且产品子表单表现在只能被主表选择
     * @return
     * @throws XbbException
     */
    private List<PaasFormDataEntityExt>  findEntitys(Map<String, Object> map, Integer businessType, Integer saasMark, PaasFormRefEntity paasFormRefEntity) throws XbbException {
        List   list = null;
        try {
            //回款单的特殊处理
            boolean sheetFlag = Objects.equals(XbbRefTypeEnum.PAYMENT_SHEET.getCode(),businessType) || Objects.equals(XbbRefTypeEnum.PAY_SHEET.getCode(),businessType);
            if(sheetFlag){
                //  只要子
                map.put(BasicConstant.ALONE_IN, SheetAloneEnum.getNotParentCodeList());
            }
            list = initDataHelp.getList(businessType,saasMark ,map, null, paasFormRefEntity);
            //TODO
        } catch (Exception e) {
            LOG.error(StatisticsServicesErrorCodeEnum.API_ERROR_224018.getMsg(),e);
            throw new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224018);
        }
        //防止使用的时候报错
        if(list == null){
            list = new ArrayList();
        }
        return list;
    }


    /**
     * 返回拼装好的k-v和k
     * @param slaveListDTO
     * @return
     */
    private Set<Object> slaveList(SlaveListDTO slaveListDTO) throws XbbException {
        Set<Object> linkSlaveSet = slaveListDTO.getSlaveSet();
        Set<Object> slaveSet = new HashSet<>();
        Set<Object> newAfilterSet = new HashSet<>();
        Map<Object, Object> oldAndNewKeyMap = new HashMap<>();
        if(linkSlaveSet.isEmpty()){
            return slaveSet;
        }
        /***==============================获取解释====================================***/
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Integer fieldType = slaveListDTO.getFieldType();
        if(FieldTypeEnum.getExplainFieldTypeEnumType().contains(fieldType)){
            Set<Object> linkSlaveSetCopy =  new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Long formId = slaveListDTO.getFormId();
            PaasFormEntityExt paasFormEntityExt = paasFormModel.getByKey(formId, slaveListDTO.getCorpid());
            if (paasFormEntityExt == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            //解释表信息
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(formId,  slaveListDTO.getCorpid());
            if (paasFormExplainEntity == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
            List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
            if(!explainList.isEmpty()){
                for (FieldAttrEntity fieldAttrEntity : explainList) {
                    String attr = fieldAttrEntity.getAttr();
                    if(Objects.equals(attr,slaveListDTO.getLinkAttrCopy())){
                        if(SaasMarkEnum.SAAS.getCode().equals(slaveListDTO.getSaasMark())){
                            List<ItemPoJo> item = proFormHelp.getItem(fieldAttrEntity, slaveListDTO.getBusinessType(), slaveListDTO.getCorpid(), slaveListDTO.getFormId());
                            fieldAttrEntity.setItems(item);
                        }
                        explainMap.put(attr, fieldAttrEntity);
                        //只有一个字段，匹配即可跳出
                        break;
                    }
                }
            }
            Iterator<Object>  iterator= linkSlaveSet.iterator();
            while (iterator.hasNext()){
                FieldAttrEntity fieldAttrEntity = explainMap.get(slaveListDTO.getLinkAttrCopy());
                List<ItemPoJo> items = fieldAttrEntity.getItems();
                if(CollectionsUtil.isNotEmpty(items)){
                    Object next = iterator.next();
                    for (ItemPoJo item : items) {
                        if(item != null){
                            if(Objects.equals(item.getText(), next)){
                                linkSlaveSetCopy.add(item.getValue());
                                iterator.remove();
                                break;
                            }
                        }
                    }
                }
            }
            if(!linkSlaveSetCopy.isEmpty()){
                linkSlaveSet.addAll(linkSlaveSetCopy);
            }
        }else if(FieldTypeEnum.numFieldType(fieldType)){
            Set<Double> doubleLinkSlaveSet = new HashSet<>();
            for (Object o : linkSlaveSet) {
                if(o == null){
                    continue;
                }
                doubleLinkSlaveSet.add(Double.parseDouble(o.toString()));
            }
            linkSlaveSet.clear();
            linkSlaveSet.addAll(doubleLinkSlaveSet);
        }else if(FieldTypeEnum.DATAID.getType().equals(fieldType)){
            Set<Long> longLinkSlaveSet = new HashSet<>();
            for (Object o : linkSlaveSet) {
                if(o == null){
                    continue;
                }
                try {
                    if(o instanceof JSONArray){
                         JSONArray  jsonArray = JSONArray.parseArray(o.toString());
                        if(jsonArray != null  && jsonArray.size()==1){
                            longLinkSlaveSet.add(Long.parseLong(jsonArray.get(0).toString()));
                        }
                    }else {
                        longLinkSlaveSet.add(Long.parseLong(o.toString()));
                    }
                } catch (NumberFormatException e) {
                    //过滤脏数据，不需要处理
                    LOG.error(slaveListDTO.getWarehouseId()+"======="+linkSlaveSet.toString(),e);
                }
            }
            linkSlaveSet.clear();
            linkSlaveSet.addAll(longLinkSlaveSet);
        }
        /***==============================获取解释====================================***/
        //这边需要兼容一下产品子表单一些特殊场景。比如退货退款产品存的是合同id,所以要把合同id转化成退货退款id。
        //为啥要写在第一张从表这边,因为主表选了产品子表，多表的话，第一张从表的业务是定死的。
        boolean tag = Objects.nonNull(slaveListDTO.getDriverBusinessType()) && Objects.equals(slaveListDTO.getDriverBusinessType(), XbbRefTypeEnum.CONTRACT_PRODUCT.getCode())
                && Objects.nonNull(slaveListDTO.getIndexTypeEnum()) && Objects.equals(slaveListDTO.getIndexTypeEnum(), IndexTypeEnum.IDX_SAAS_REFUND);
        if (tag) {
            newAfilterSet = handSpecialSubProduct(slaveListDTO.getDriverBusinessType(), linkSlaveSet, slaveListDTO.getIndexTypeEnum(), slaveListDTO, oldAndNewKeyMap);
        }
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        String slaveKey = slaveListDTO.getLinkAttrCopy();

        int inData = slaveListDTO.getInData();
        String filterKey = slaveKey;
        String esSlaveKey = slaveKey;
        String id = "id";
        if(id.equals(esSlaveKey)){
            esSlaveKey = "dataId";
        }
        if (inData == 1) {
            filterKey = "data." + esSlaveKey;
        }else {
            filterKey = esSlaveKey;
        }
        if(CollectionsUtil.isEmpty(linkSlaveSet)){
            LOG.error("关联集合为空"+"_"+filterKey);
            LOG.error(slaveListDTO.toString());
            return slaveSet;
        }
        //TODO  这里要判断字段类型，如果是String，可能要加.keyword
        filterKey = EsUtil.addKeyWord(slaveListDTO.getFieldType(), filterKey);
        boolQueryBuilder
                .filter(termQuery("corpid.keyword", slaveListDTO.getCorpid()))
                .filter(termQuery("formId", slaveListDTO.getFormId()))
                .filter(termsQuery(filterKey, (Objects.isNull(newAfilterSet) || newAfilterSet.size() == 0) ? linkSlaveSet : newAfilterSet));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,slaveListDTO.getIndexTypeEnum().getType()));
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(boolQueryBuilder).fetchSource(new String[]{"dataId","corpid","data",esSlaveKey }, null);
        SearchRequest searchRequest = new SearchRequest(slaveListDTO.getIndexTypeEnum().getIndex());
        EsUtil.setPage(sourceBuilder, 1, EsNormalConstant.FIND_NUM);
        searchRequest.source(sourceBuilder);
        //这里只有一页，因为外面做了限定
        List<PaasFormDataEntityExt> content  = xbbElasticsearchRestTemplate.findScrollList(PaasFormDataEntityExt.class,searchRequest,SizeConstant.SCROLL_TIME_IN_MILLIS);
        Map<Object, PaasFormDataEntityExt> slaveMap = slaveListDTO.getSlavesMap();
        Set<Long> dataIdSet = new HashSet<>();
        for (PaasFormDataEntityExt paasFormDataEsEntity : content) {
            Object key;
            if (inData == 0) {
                key = ReflectHelper.valueGet(paasFormDataEsEntity, esSlaveKey,true);
            } else {
                JSONObject data = paasFormDataEsEntity.getData();
                if(data == null || data.isEmpty()){
                    //TODO 是不是应该报错而不是跳过？？？
                    continue;
                }
                key = data.get(esSlaveKey);
            }
            //放在下面去获取数据库
            slaveSet.add(key);
            dataIdSet.add(paasFormDataEsEntity.getDataId());
        }
        //TODO 查询详细数据
        Map<String, Object> map =  BeanUtil.convertBean2Map(slaveListDTO,true);
        dataIdSet.add(-1L);
        map.put("idIn",dataIdSet);
        List<PaasFormDataEntityExt> driverList = findEntitys(map, slaveListDTO.getBusinessType(), slaveListDTO.getSaasMark(), null);
        for (PaasFormDataEntityExt dataEntityExt : driverList) {
            //此处的key是字段存储的值，拿到后面做
            if (Objects.isNull(dataEntityExt.getDataId())){
                dataEntityExt.setDataId(dataEntityExt.getId());
            }
            Object key;
            dataEntityExt.setData(dataEntityExt.getData());
            dataEntityExt.setDataId(dataEntityExt.getId());
            if (inData == 0) {
                key = ReflectHelper.valueGet(dataEntityExt, slaveKey,true);
            } else {
                key = dataEntityExt.getData().get(slaveKey);
            }
            if(key == null){
                continue;
            }
            dataEntityExt.setData(dataEntityExt.getData());
            key = refacterLinkValue(explainMap, fieldType, key, slaveListDTO.getLinkAttrCopy());
            //key统一转化成String,方便获取比较
            if (tag) {
                Object newKey = oldAndNewKeyMap.get(key);
                slaveMap.put(newKey.toString(), dataEntityExt);
            }else {
                slaveMap.put(key.toString(), dataEntityExt);
            }
        }
        slaveListDTO.setSlavesMap(slaveMap);
        //团队数据的处理
        if( SaasMarkEnum.SAAS.getCode().equals(slaveListDTO.getSaasMark())){
            BITeamEnum bITeamEnum = BITeamEnum.getByParentCode(slaveListDTO.getBusinessType());
            if(bITeamEnum !=null){
                XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(bITeamEnum.getCode());
                Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                params.put("orderByStr", "id");
                params.put("dataIdIn", dataIdSet);
                params.put("corpid", slaveListDTO.getCorpid());
                params.put("pageSize", SizeConstant.PAGE_SIZE);
                Map<Long,Set<UserTeamEntity>> userTeamMap = slaveListDTO.getSlavesUserMap();
                boolean teamFlag = true;
                while (teamFlag) {
                    List teamList = null;
                    teamList = teamHelp.getList(xbbRefTypeEnum, params, teamList);
                    if (teamList == null || teamList.isEmpty()) {
                        teamFlag = false;
                    } else {
                        //大于上次最大的id
                        teamHelp.setIdGte(xbbRefTypeEnum, params, teamList);
                        for (Object object : teamList) {
                            UserTeamEntity userTeamEntity = (UserTeamEntity)object;
                            Long dataId = userTeamEntity.getDataId();
                            Set<UserTeamEntity> userTeamEntitySet;
                            if(userTeamMap.containsKey(dataId)){
                                userTeamEntitySet = userTeamMap.get(dataId);
                                if(userTeamEntitySet == null){
                                    userTeamEntitySet = new HashSet<>();
                                }
                            }else {
                                userTeamEntitySet = new HashSet<>();
                            }
                            userTeamEntitySet.add(userTeamEntity);
                            userTeamMap.put(dataId,userTeamEntitySet);
                        }
                    }
                }
                slaveListDTO.setSlavesUserMap(userTeamMap);
            }
        }
        return slaveSet;
    }

    /**
     * 改变 关联字段值
     * 例如  text: "初步接触", value: "caa5ff26-d810-4fa9-3d64-171b4b83cb00"
     * @param explainMap
     * @param fieldType
     * @param key
     * @param linkAttrCopy
     * @return
     */
    private Object refacterLinkValue(Map<String, FieldAttrEntity> explainMap, Integer fieldType, Object key, String linkAttrCopy) {
        if (FieldTypeEnum.getExplainFieldTypeEnumType().contains(fieldType)) {
            FieldAttrEntity fieldAttrEntity = explainMap.get(linkAttrCopy);
            //防止字段被删掉，报空指针
            if(Objects.isNull(fieldAttrEntity)){
                return key;
            }
            List<ItemPoJo> items = fieldAttrEntity.getItems();
            if (CollectionsUtil.isNotEmpty(items)) {
                for (ItemPoJo item : items) {
                    if (item != null) {
                        if (Objects.equals(item.getValue(), key)) {
                            key = item.getText();
                            break;
                        }
                    }
                }
            }
        }
        return key;
    }

    private JSONObject getJsonObject(String pre) {
        //外层
        JSONObject outJsonObject = new JSONObject();
        //内层
        JSONObject subFormJson = new JSONObject();
        //公有部分
        JSONObject newJsonObject = new JSONObject();
        //内层数量减半
        JSONObject inJsonObject = new JSONObject();

        for (int i = 1; i <= FieldTypeEnum.TEXT.getMax(); i++) {
            newJsonObject.put(pre+"data.text_"+i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_STRING_SORT));
        }
        for (int i = 1; i <= FieldTypeEnum.NUM.getMax(); i++) {
            newJsonObject.put(pre+"data.num_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DOUBLE));
        }
        for (int i = 1; i <= FieldTypeEnum.DATETIME.getMax(); i++) {
            newJsonObject.put(pre+"data.date_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DATE_SECOND));
        }
        for (int i = 1; i <= FieldTypeEnum.LOCATION.getMax(); i++) {
            newJsonObject.put(pre+"data.geo_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_LOCATION));
        }
        for (int i = 1; i <= FieldTypeEnum.ADDRESS.getMax(); i++) {
            newJsonObject.put(pre+"data.address_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_ADDRESS));
        }
        for (int i = 1; i <= FieldTypeEnum.UPLOAD.getMax(); i++) {
            newJsonObject.put(pre+"data.file_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_TEXT));
        }
        //数组类型 内部定义为
        for (int i = 1; i <= FieldTypeEnum.USER_GROUP.getMax(); i++) {
            newJsonObject.put(pre+"data.array_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_TEXT));
        }
        //子表单
        for (int i = 1; i <= SubFieldTypeEnum.TEXT.getMax(); i++) {
            inJsonObject.put("text_"+i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_STRING_SORT));
        }
        for (int i = 1; i <= SubFieldTypeEnum.NUM.getMax(); i++) {
            inJsonObject.put("num_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DOUBLE));
        }
        for (int i = 1; i <= SubFieldTypeEnum.DATETIME.getMax(); i++) {
            inJsonObject.put("date_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DATE_SECOND));
        }
        for (int i = 1; i <= SubFieldTypeEnum.LOCATION.getMax(); i++) {
            inJsonObject.put("geo_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_LOCATION));
        }
        for (int i = 1; i <= SubFieldTypeEnum.ADDRESS.getMax(); i++) {
            inJsonObject.put("address_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_ADDRESS));
        }
        for (int i = 1; i <= SubFieldTypeEnum.UPLOAD.getMax(); i++) {
            inJsonObject.put("file_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_TEXT));
        }
        //数组类型
        for (int i = 1; i <= SubFieldTypeEnum.USERGROUP.getMax(); i++) {
            inJsonObject.put("array_" + i, (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_TEXT));
        }
        subFormJson.put("properties",inJsonObject);
        //子表单数量限制3个
        for (int i = 1; i <= FieldTypeEnum.SUB_FORM.getMax(); i++) {
            outJsonObject.put(pre+"data.subForm_" + i, subFormJson);
        }
        //addTime
        outJsonObject.put(pre+"addTime", (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DATE_SECOND));
        //lastConnectTime
        outJsonObject.put(pre+"updateTime", (JSONObject) JSONObject.parse(EsMappingConstant.MAPPING_FOR_DATE_SECOND));

        outJsonObject.putAll(newJsonObject);
        return outJsonObject;
    }
    /**
     * 构建插入列表
     * @param corpid
     * @return
     */
    private List<StatisticPaasFormDataEsEntity> structureInsertList(String corpid, boolean hasSecondSlave, String atlinkAttr , BiRelTableEnum biRelTableEnum, List<SlaveSourcePojo> slaveSourceListForCorrelation, Long warehouseEntityId, Integer bLocation, List<PaasFormDataEntityExt> driverList, Integer driverInData, Map<Object, PaasFormDataEntityExt> bMap, Map<Object, PaasFormDataEntityExt> cMap, List<InsertDTO> insertList,Map<String, FieldAttrEntity> explainMap,Integer fieldType,String secondLinkAttr , Integer secondFieldType,Integer driverSecondInData) throws XbbException {
        List<StatisticPaasFormDataEsEntity>  list =  new ArrayList<>();
        for (PaasFormDataEntityExt dataEntityExt : driverList) {
            Object key ;
            if(Objects.equals(0,driverInData)){
                key =  ReflectHelper.valueGet(dataEntityExt, atlinkAttr,true);
            }else {
                key = dataEntityExt.getData().get(atlinkAttr);
            }
            if(key == null){
                key ="";
            }
            if(key instanceof JSONArray){
                JSONArray  jsonArray = JSONArray.parseArray(key.toString());
                if(jsonArray != null  && jsonArray.size()==1){
                    key = jsonArray.get(0);
                }
            }else {
                key = refacterLinkValue(explainMap, fieldType, key, atlinkAttr);
            }
            //有第三张表
            Object secondKey = "";
            if(hasSecondSlave){
                if(Objects.equals(0,driverSecondInData)){
                    secondKey =  ReflectHelper.valueGet(dataEntityExt,secondLinkAttr,true);
                }else {
                    secondKey = dataEntityExt.getData().get(secondLinkAttr);
                }
                if(secondKey == null){
                    secondKey = "";
                }
                //TODO
                secondKey = refacterLinkValue(explainMap, secondFieldType, secondKey, secondLinkAttr);
            }
            StatisticPaasFormDataEsEntity statisticPaasFormDataEsEntity = new StatisticPaasFormDataEsEntity();
            statisticPaasFormDataEsEntity.setCorpid(corpid);
            //区分同一个索引下的不同表
            statisticPaasFormDataEsEntity.setWarehouseId(warehouseEntityId);
//            statisticPaasFormDataEsEntity.putAtEntity(dataEntityExt);

            // 要确定B、C的位置
            //key统一转化成String,方便获取比较
            PaasFormDataEntityExt secondEntity = bMap.get(key.toString());
            //key统一转化成String,方便获取比较
            PaasFormDataEntityExt thirdEntity = cMap.get(secondKey.toString());
            //当从表都不存在的时候，不入库。防止后面关联了B表进不来
            if (secondEntity == null && thirdEntity == null) {
                    continue;
            }
            //设置formId保证查询的时候知道的索引里是更新还是新建  暂时不用
            if(FormLocationEnum.B.getCode() == bLocation){
                statisticPaasFormDataEsEntity.putBtEntity(secondEntity);
                statisticPaasFormDataEsEntity.putCtEntity(thirdEntity);
            }else if(FormLocationEnum.C.getCode() == bLocation){
                statisticPaasFormDataEsEntity.putBtEntity(thirdEntity);
                statisticPaasFormDataEsEntity.putCtEntity(secondEntity);
            }
            InsertDTO insertDTO = new InsertDTO();
            //  esId 去除uuid的使用    corpid+"_"+adataId+"_"+warehouseEntityId可以唯一确定关联关系
//            String esId = UUID.randomUUID().toString();
            String esId = corpid+"_"+statisticPaasFormDataEsEntity.getAtdataId()+"_"+ warehouseEntityId;
            statisticPaasFormDataEsEntity.setEsId(esId);
            insertDTO.setEsId(esId);
            insertDTO.setSource(BeanUtil.convertBean2Map(statisticPaasFormDataEsEntity));

            insertDTO.setIndex(biRelTableEnum.getIndex());
            insertDTO.setType(biRelTableEnum.getType());
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertList.add(insertDTO);
            list.add(statisticPaasFormDataEsEntity);
        }
        return list;
    }
    private List<StatisticBiFormDataEntity> structureBiInsertList(StructureBiInsertDTO structureBiInsertDTO) throws XbbException {
        Map<Long,List<Long>>  slaveBIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long,List<Long>>  slaveCIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long,List<Long>>  slaveDIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long,List<Long>>  slaveEIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long,List<Long>>  slaveFIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<StatisticBiFormDataEntity>  list =  new ArrayList<>();
        //选中的字段
        JSONObject formIdAttr = structureBiInsertDTO.getFormIdAttr();
        Long formId = structureBiInsertDTO.getDriverList().get(0).getFormId();
        if(BIProductReformEnum.getProductSubBusList().contains(structureBiInsertDTO.getDriverBusinessType())){
            if(CollectionsUtil.isNotEmpty(structureBiInsertDTO.getSlaveSourceListForCorrelation())){
                formId = structureBiInsertDTO.getDriverFormId();
            }
        }
        JSONObject kvJsonObject = (JSONObject) formIdAttr.get(formId.toString());
        for (PaasFormDataEntityExt dataEntityExt : structureBiInsertDTO.getDriverList()) {
            JSONObject statisticBiJson = new JSONObject();
            try {
                Object key ="";
                if(CollectionsUtil.isNotEmpty(structureBiInsertDTO.getSlaveSourceListForCorrelation())){
                    if(Objects.equals(0, structureBiInsertDTO.getDriverInData())){
                        key =  ReflectHelper.valueGet(dataEntityExt, structureBiInsertDTO.getAtlinkAttr(),true);
                    }else {
                        key = dataEntityExt.getData().get(structureBiInsertDTO.getAtlinkAttr());
                    }
                    if(key == null){
                        key ="";
                    }
                    if(key instanceof JSONArray){
                        JSONArray  jsonArray = JSONArray.parseArray(key.toString());
                        if(jsonArray != null  && jsonArray.size()==1){
                            key = jsonArray.get(0);
                        }
                    }else {
                        key = refacterLinkValue(structureBiInsertDTO.getExplainMap(), structureBiInsertDTO.getFieldType(), key, structureBiInsertDTO.getAtlinkAttr());
                    }
                }
                //有第三张表
                Object secondKey = "";
                if(structureBiInsertDTO.getHasSecondSlave()){
                    if(Objects.equals(0, structureBiInsertDTO.getDriverSecondInData())){
                        secondKey =  ReflectHelper.valueGet(dataEntityExt, structureBiInsertDTO.getSecondLinkAttr(),true);
                    }else {
                        secondKey = dataEntityExt.getData().get(structureBiInsertDTO.getSecondLinkAttr());
                    }
                    if(secondKey == null){
                        secondKey = "";
                    }
                    //TODO
                    secondKey = refacterLinkValue(structureBiInsertDTO.getExplainMap(), structureBiInsertDTO.getSecondFieldType(), secondKey, structureBiInsertDTO.getSecondLinkAttr());
                }
                //第三张从属表
                Object cKey = "";
                if(structureBiInsertDTO.getSlaveSourceListForCorrelation().size()>2){
                    if(Objects.equals(0, structureBiInsertDTO.getLinkAttrCInData())){
                        cKey =  ReflectHelper.valueGet(dataEntityExt, structureBiInsertDTO.getLinkAttrC(),true);
                    }else {
                        cKey = dataEntityExt.getData().get(structureBiInsertDTO.getLinkAttrC());
                    }
                    if(cKey == null){
                        cKey = "";
                    }
                    //TODO
                    cKey = refacterLinkValue(structureBiInsertDTO.getExplainMap(), structureBiInsertDTO.getLinkAttrCFieldType(), cKey, structureBiInsertDTO.getLinkAttrC());
                }
                //第四张从属表
                Object dKey = "";
                if(structureBiInsertDTO.getSlaveSourceListForCorrelation().size()>3){
                    if(Objects.equals(0, structureBiInsertDTO.getLinkAttrDInData())){
                        dKey =  ReflectHelper.valueGet(dataEntityExt, structureBiInsertDTO.getLinkAttrD(),true);
                    }else {
                        dKey = dataEntityExt.getData().get(structureBiInsertDTO.getLinkAttrD());
                    }
                    if(dKey == null){
                        dKey = "";
                    }
                    //TODO
                    dKey = refacterLinkValue(structureBiInsertDTO.getExplainMap(), structureBiInsertDTO.getLinkAttrDFieldType(), dKey, structureBiInsertDTO.getLinkAttrD());
                }
                //第五张从属表
                Object eKey = "";
                if(structureBiInsertDTO.getSlaveSourceListForCorrelation().size()>4){
                    if(Objects.equals(0, structureBiInsertDTO.getLinkAttrEInData())){
                        eKey =  ReflectHelper.valueGet(dataEntityExt, structureBiInsertDTO.getLinkAttrE(),true);
                    }else {
                        eKey = dataEntityExt.getData().get(structureBiInsertDTO.getLinkAttrE());
                    }
                    if(eKey == null){
                        eKey = "";
                    }
                    //TODO
                    eKey = refacterLinkValue(structureBiInsertDTO.getExplainMap(), structureBiInsertDTO.getLinkAttrEFieldType(), eKey, structureBiInsertDTO.getLinkAttrE());
                }
                //使用json赋值
                for (String s : kvJsonObject.keySet()) {
                    Object value;
                    String biKey = kvJsonObject.getString(s);
                    if(s.contains("_")){
                        String  sc = s;
                        if(s.contains(".")){
                            String[] address = s.split("\\.");
                            sc = address[0];
                            //bi大表对应的key
                            biKey =  biKey.split("\\.")[0];
                        }
                        value = dataEntityExt.getData().get(sc);
                    }else {
                        value = ReflectHelper.valueGet(dataEntityExt,s,true);
                    }
                    statisticBiJson.put(biKey, value);
                }
                //key统一转化成String,方便获取比较
                PaasFormDataEntityExt secondEntity = structureBiInsertDTO.getBMap().get(key.toString());
                //key统一转化成String,方便获取比较
                PaasFormDataEntityExt thirdEntity = structureBiInsertDTO.getCMap().get(secondKey.toString());
                PaasFormDataEntityExt cEntity = structureBiInsertDTO.getDMap().get(cKey.toString());
                PaasFormDataEntityExt dEntity = structureBiInsertDTO.getEMap().get(dKey.toString());
                PaasFormDataEntityExt eEntity = structureBiInsertDTO.getFMap().get(eKey.toString());
                //当从表都不存在的时候，不入库。防止后面关联了B表进不来  只有负责人的单表除外
                if(CollectionsUtil.isNotEmpty(structureBiInsertDTO.getSlaveSourceListForCorrelation())){
                    if (secondEntity == null && thirdEntity == null && cEntity == null && dEntity == null && eEntity == null) {
                        continue;
                    }
                }
                integrateStasticBiFormData(formIdAttr, statisticBiJson, secondEntity);
                integrateStasticBiFormData(formIdAttr, statisticBiJson, thirdEntity);
                integrateStasticBiFormData(formIdAttr, statisticBiJson, cEntity);
                integrateStasticBiFormData(formIdAttr, statisticBiJson, dEntity);
                integrateStasticBiFormData(formIdAttr, statisticBiJson, eEntity);
                //团队的处理
                structUserTeam(statisticBiJson,  structureBiInsertDTO.getAUserMap().get(dataEntityExt.getId()),FormLocationEnum.A);
                if(structureBiInsertDTO.getBUserMap() != null && secondEntity !=null){
                    structUserTeam(statisticBiJson,  structureBiInsertDTO.getBUserMap().get(secondEntity.getId()),FormLocationEnum.B);
                }
                if(structureBiInsertDTO.getCUserMap() != null && thirdEntity !=null){
                    structUserTeam(statisticBiJson,  structureBiInsertDTO.getCUserMap().get(thirdEntity.getId()),FormLocationEnum.C);
                }
                if(structureBiInsertDTO.getDUserMap() != null && cEntity != null){
                    structUserTeam(statisticBiJson,  structureBiInsertDTO.getDUserMap().get(cEntity.getId()),FormLocationEnum.D);
                }
                if(structureBiInsertDTO.getEUserMap() != null && dEntity != null){
                    structUserTeam(statisticBiJson,  structureBiInsertDTO.getEUserMap().get(dEntity.getId()),FormLocationEnum.E);
                }
                if(structureBiInsertDTO.getEUserMap() != null && eEntity != null){
                    structUserTeam(statisticBiJson,  structureBiInsertDTO.getFUserMap().get(eEntity.getId()),FormLocationEnum.F);
                }

                //通过反射的方式赋值回来
                StatisticBiFormDataEntity statisticBiFormDataEntity  =  statisticBiJson.toJavaObject(StatisticBiFormDataEntity.class);
                setSlaveIdMap(slaveBIdMap, statisticBiFormDataEntity.getDataId_1(), statisticBiFormDataEntity.getDataId_2());
                if(statisticBiFormDataEntity.getDataId_3() != null){
                    setSlaveIdMap(slaveCIdMap, statisticBiFormDataEntity.getDataId_1(), statisticBiFormDataEntity.getDataId_3());
                }
                if(statisticBiFormDataEntity.getDataId_4() != null){
                    setSlaveIdMap(slaveDIdMap, statisticBiFormDataEntity.getDataId_1(), statisticBiFormDataEntity.getDataId_4());
                }
                if(statisticBiFormDataEntity.getDataId_5() != null){
                    setSlaveIdMap(slaveEIdMap, statisticBiFormDataEntity.getDataId_1(), statisticBiFormDataEntity.getDataId_5());
                }
                if(statisticBiFormDataEntity.getDataId_6() != null){
                    setSlaveIdMap(slaveFIdMap, statisticBiFormDataEntity.getDataId_1(), statisticBiFormDataEntity.getDataId_6());
                }

                //设置更新时间
                statisticBiFormDataEntity.setUpdateTime(DateUtil.getNow());
                statisticBiFormDataEntity.setCorpid(structureBiInsertDTO.getCorpid());
                //区分同一个索引下的不同表
                statisticBiFormDataEntity.setWarehouseId(structureBiInsertDTO.getWarehouseEntityId());
                //  esId 去除uuid的使用    corpid+"_"+adataId+"_"+warehouseEntityId可以唯一确定关联关系
                String esId = structureBiInsertDTO.getCorpid() +"_"+dataEntityExt.getDataId()+"_"+ structureBiInsertDTO.getWarehouseEntityId();
                statisticBiFormDataEntity.setEsId(esId);
                String dir = ProBaseConfig.getProjectName();
                //TODO  暂时注释，刷完数据再开始
                statisticBiFormDataEntity.setParent(esId);
                if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                    //设置formId保证查询的时候知道的索引里是更新还是新建  暂时不用
                    InsertDTO insertDTO = new InsertDTO();
                    insertDTO.setEsId(esId);
                    Map<String, Object> source = BeanUtil.convertBean2Map(statisticBiFormDataEntity);
                    insertDTO.setSource(source);
                    source.put(ElasticConstant.MY_JOIN_FIELD, structureBiInsertDTO.getBiRelTableEnum().getType());
                    insertDTO.setIndex(structureBiInsertDTO.getBiRelTableEnum().getIndex());
                    insertDTO.setType(structureBiInsertDTO.getBiRelTableEnum().getType());
                    insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
                    structureBiInsertDTO.getInsertList().add(insertDTO);
                }
                statisticBiFormDataEntity.setMy_join_field(structureBiInsertDTO.getBiRelTableEnum().getType());
                list.add(statisticBiFormDataEntity);
            } catch (Exception e) {
                LOG.error("===="+statisticBiJson.toJSONString(),e);
            }
        }
        structureBiInsertDTO.setSlaveBIdMap(slaveBIdMap);
        structureBiInsertDTO.setSlaveCIdMap(slaveCIdMap);
        structureBiInsertDTO.setSlaveDIdMap(slaveDIdMap);
        structureBiInsertDTO.setSlaveEIdMap(slaveEIdMap);
        structureBiInsertDTO.setSlaveFIdMap(slaveFIdMap);
        return list;
    }
    
    private void setSlaveIdMap(Map<Long, List<Long>> slaveBIdMap, Long dataIdOne, Long dataId_2) {
        List<Long> slaveBIdList = slaveBIdMap.get(dataId_2);
        if(slaveBIdList == null){
            slaveBIdList = new ArrayList<>();
        }
        slaveBIdList.add(dataIdOne);
        slaveBIdMap.put(dataId_2,slaveBIdList);
    }

    private void structUserTeam(JSONObject statisticBiJson, Set<UserTeamEntity> aUserTeam,FormLocationEnum formLocationEnum ) {
        structUserTeam(statisticBiJson, aUserTeam, formLocationEnum.getCode());
    }

    private void structUserTeam(JSONObject statisticBiJson, Set<UserTeamEntity> aUserTeam, int code) {
        if(CollectionsUtil.isNotEmpty(aUserTeam)){
            statisticBiJson.put("myTeam_"+ code,(JSONArray)JSONArray.toJSON(aUserTeam));
            Set<UserTeamEntity> userTeamMain = new HashSet<>();
            for (UserTeamEntity userTeamEntity : aUserTeam) {
                if(Objects.equals(userTeamEntity.getDel(),0) && Objects.equals(userTeamEntity.getIsMain(),1)){
                    userTeamMain.add(userTeamEntity);
                }
            }
            if(CollectionsUtil.isNotEmpty(userTeamMain)){
                statisticBiJson.put("myTeamMain_"+ code,(JSONArray)JSONArray.toJSON(userTeamMain));
            }
        }
    }


    /**
     *根据表单数据整合BI数据
     * @param formIdAttr  字段对应关系
     * @param statisticBiJson   整合好的数据
     * @param dataEntityExt  表单数据
     */
    private void integrateStasticBiFormData(JSONObject formIdAttr, JSONObject statisticBiJson, PaasFormDataEntityExt dataEntityExt) {
        integrateStasticBiFormData(formIdAttr, statisticBiJson, dataEntityExt,null);
    }

    private void integrateStasticBiFormData(JSONObject formIdAttr, JSONObject statisticBiJson, PaasFormDataEntityExt dataEntityExt,SlaveSourcePojo atJsonPojo) {
        if(dataEntityExt == null){
            return;
        }
        String key = dataEntityExt.getFormId().toString();
        if(atJsonPojo!=null && Objects.equals(atJsonPojo.getBusinessType(),XbbRefTypeEnum.CONTRACT_PRODUCT.getCode())){
            key = atJsonPojo.getFormId().toString();
        }
        JSONObject attrJSONObject = formIdAttr.getJSONObject(key);
        for (String s : attrJSONObject.keySet()) {
            Object value;
            String biKey = attrJSONObject.getString(s);
            if(s.contains("_")){
                String  sc = s;
                if(s.contains(".")){
                    String[] address = s.split("\\.");
                    sc = address[0];
                    //bi大表对应的key
                    biKey =  biKey.split("\\.")[0];
                }
                value = dataEntityExt.getData().get(sc);
            }else {
                value = ReflectHelper.valueGet(dataEntityExt,s,true);
            }
            statisticBiJson.put(biKey,value);
        }
    }

    private void setLoaction(String corpid, BiRelTableEnum biRelTableEnum, long now, List<BiDataCorrelationEntity> insertCorrelationList, List<BiDataCorrelationEntity> updateCorrelationList, int i, SlaveSourcePojo innerSlaveSourcePojo, BiDataCorrelationEntity biDataCorrelationEntity) {
        if(biDataCorrelationEntity == null){
            //插入
            biDataCorrelationEntity = new BiDataCorrelationEntity();
            BeanUtil.copyProperties(innerSlaveSourcePojo,biDataCorrelationEntity);
            biDataCorrelationEntity.setLinkAttr(innerSlaveSourcePojo.getLinkAttrCopy());
            biDataCorrelationEntity.setSecondLinkAttr(innerSlaveSourcePojo.getSecondLinkAttrCopy());
            biDataCorrelationEntity.setAddTime(now);
            biDataCorrelationEntity.setUpdateTime(now);
            biDataCorrelationEntity.setCorpid(corpid);
            biDataCorrelationEntity.setIsCorrelation(1);
            biDataCorrelationEntity.setRelTable(biRelTableEnum.getCode());
            //关联位置
            biDataCorrelationEntity.setLocation(i+ 1);
            insertCorrelationList.add(biDataCorrelationEntity);
        }else {
            // 更新
            if(biDataCorrelationEntity.getDel() == 1){
                //被删除过，IsCorrelation置为1，del改为0
                biDataCorrelationEntity.setIsCorrelation(1);
                biDataCorrelationEntity.setDel(0);
            }else {
                biDataCorrelationEntity.setIsCorrelation(biDataCorrelationEntity.getIsCorrelation() + 1);
            }
            updateCorrelationList.add(biDataCorrelationEntity);
        }
        innerSlaveSourcePojo.setLocation(biDataCorrelationEntity.getLocation());
    }

    private void setLoaction(String corpid, BiRelTableEnum biRelTableEnum, long now, List<BiDataCorrEntity> insertCorrelationList, List<BiDataCorrEntity> updateCorrelationList, int location, SlaveSourcePojo innerSlaveSourcePojo, BiDataCorrEntity biDataCorrEntity) {
        if(biDataCorrEntity == null){
            //插入
            biDataCorrEntity = new BiDataCorrEntity();
            BeanUtil.copyProperties(innerSlaveSourcePojo,biDataCorrEntity);
            biDataCorrEntity.setLinkAttr(innerSlaveSourcePojo.getLinkAttrCopy());
            //主表的特殊处理
            if(Objects.equals(0,location)){
                //当大于2张表关联的时候
                if(StringUtil.isNotEmpty(innerSlaveSourcePojo.getLinkAttrBCopy())){
                    biDataCorrEntity.setLinkAttrB(innerSlaveSourcePojo.getLinkAttrBCopy());
                    biDataCorrEntity.setLinkAttrC(innerSlaveSourcePojo.getLinkAttrCCopy());
                    biDataCorrEntity.setLinkAttrD(innerSlaveSourcePojo.getLinkAttrDCopy());
                    biDataCorrEntity.setLinkAttrE(innerSlaveSourcePojo.getLinkAttrECopy());
                    //放置serial
                    String serial = MD5Util.EncoderByMd5(innerSlaveSourcePojo.getLinkAttrBCopy() + "_" + innerSlaveSourcePojo.getLinkAttrCCopy() + "_" + innerSlaveSourcePojo.getLinkAttrDCopy() + "_" + innerSlaveSourcePojo.getLinkAttrECopy());
                    biDataCorrEntity.setLinkAttrSerial(serial);
                }
            }
            biDataCorrEntity.setAddTime(now);
            biDataCorrEntity.setUpdateTime(now);
            biDataCorrEntity.setCorpid(corpid);
            biDataCorrEntity.setIsCorrelation(1);
            biDataCorrEntity.setRelTable(biRelTableEnum.getCode());
            //关联位置
            biDataCorrEntity.setLocation(location+ 1);
            insertCorrelationList.add(biDataCorrEntity);
        }else {
            // 更新
            if(biDataCorrEntity.getDel() == 1){
                //被删除过，IsCorrelation置为1，del改为0
                biDataCorrEntity.setIsCorrelation(1);
                biDataCorrEntity.setDel(0);
            }else {
                biDataCorrEntity.setIsCorrelation(biDataCorrEntity.getIsCorrelation() + 1);
            }
            updateCorrelationList.add(biDataCorrEntity);
        }
        innerSlaveSourcePojo.setLocation(biDataCorrEntity.getLocation());
    }

    /**
     * 增量
     * @param updateList
     * @param relTable
     * @param nowFormIdUpdateIdList
     * @param biRelTableEnum
     * @throws XbbException
     */
    private void incrData(List<PaasFormDataEntityExt> updateList, Integer relTable, List<Long> nowFormIdUpdateIdList, BiRelTableEnum biRelTableEnum) throws XbbException {
        List<PaasFormDataEntityExt> driverList = new ArrayList<>();
        for (PaasFormDataEntityExt dataEntityExt : updateList) {
            if(nowFormIdUpdateIdList.contains(dataEntityExt.getId())){
                driverList.add(dataEntityExt);
            }
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("relTable",relTable);
        //TODO
        //查询结果可能大于1,因为一张表单可能用同一个字段在不同大表中做主表
        List<BiProWarehouseEntity> warehouseList = biProWarehouseModel.findEntitys(param);
        if(CollectionsUtil.isNotEmpty(warehouseList)){
            for (BiProWarehouseEntity biProWarehouseEntity : warehouseList) {
                try {
                    /***==============================获取解释====================================***/
                    Map<String, FieldAttrEntity> explainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Integer fieldType = biProWarehouseEntity.getAtfieldType();
                    Integer secondFieldType = biProWarehouseEntity.getAtSecondFieldType();
                    if(FieldTypeEnum.getExplainFieldTypeEnumType().contains(fieldType) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(secondFieldType)){
                        Long formId = biProWarehouseEntity.getAtformId();
                        PaasFormEntityExt paasFormEntityExt = paasFormModel.getByKey(formId, biProWarehouseEntity.getCorpid());
                        if (paasFormEntityExt == null) {
                            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
                        }
                        //解释表信息
                        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(formId, biProWarehouseEntity.getCorpid());
                        if (paasFormExplainEntity == null) {
                            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
                        }
                        List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                        if(!explainList.isEmpty()){
                            for (FieldAttrEntity fieldAttrEntity : explainList) {
                                String attr = fieldAttrEntity.getAttr();
                                if(SaasMarkEnum.SAAS.getCode().equals(biProWarehouseEntity.getAtsaasMark())) {
                                    List<ItemPoJo> item = proFormHelp.getItem(fieldAttrEntity, biProWarehouseEntity.getAtbusinessType(), biProWarehouseEntity.getCorpid(), formId);
                                    fieldAttrEntity.setItems(item);
                                }
                                explainMap.put(attr, fieldAttrEntity);
                            }
                        }
                    }
                    /***==============================获取解释====================================***/
                    Set<Object> aFilterSet = new HashSet<>();
                    Set<Object> aSecondFilterSet = new HashSet<>();
                    String atlinkAttr = biProWarehouseEntity.getAtlinkAttr();
                    String atSecondLinkAttr = biProWarehouseEntity.getAtSecondLinkAttr();
                    //可能不存在第二张从表
                    Long ctformId = biProWarehouseEntity.getCtformId();
                    String ctlinkAttr = biProWarehouseEntity.getCtlinkAttr();
                    Integer ctInData = biProWarehouseEntity.getCtinData();
                    boolean hasSecondSlave = false;
                    if(ctformId !=null && StringUtil.isNotEmpty(ctlinkAttr) && ctInData !=null) {
                        hasSecondSlave = true;
                    }

                    Map<Object,PaasFormDataEntityExt> map1 = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Object,PaasFormDataEntityExt> map2 = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    for (PaasFormDataEntityExt dataEntityExt : driverList) {
                        Object key ;
                        dataEntityExt.setData(dataEntityExt.getData());
                        if(Objects.equals(0,biProWarehouseEntity.getAtinData())){
                            key =  ReflectHelper.valueGet(dataEntityExt, atlinkAttr,true);
                        }else {
                            key = dataEntityExt.getData().get(atlinkAttr);
                        }
                        if(key == null){
                            continue;
                        }
                        key = refacterLinkValue(explainMap, fieldType, key, atlinkAttr);
                        aFilterSet.add(key);
                        if(hasSecondSlave){
                            Object secondKey ;
                            if(Objects.equals(0,biProWarehouseEntity.getAtSecondInData())){
                                secondKey =  ReflectHelper.valueGet(dataEntityExt,atSecondLinkAttr,true);
                            }else {
                                secondKey = dataEntityExt.getData().get(atSecondLinkAttr);
                            }
                            if(secondKey == null){
                                continue;
                            }
                            secondKey = refacterLinkValue(explainMap, secondFieldType, secondKey, atSecondLinkAttr);
                            aSecondFilterSet.add(secondKey);
                            map2.put(secondKey,dataEntityExt);
                        }
                        map1.put(key,dataEntityExt);
                    }
                    //aFilterlist不为空再往下进行
                    if(aFilterSet.isEmpty()){
                        continue;
                    }
                    Map<Object,PaasFormDataEntityExt> bMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    Map<Object,PaasFormDataEntityExt> cMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    IndexTypeEnum firstIndexTypeEnum =  indexTypeModel.getIndexType(biProWarehouseEntity.getCorpid(), biProWarehouseEntity.getBtbusinessType(), biProWarehouseEntity.getBtsaasMark());
                    SlaveListDTO slaveListDTO =
                            new SlaveListDTO(bMap, aFilterSet, biProWarehouseEntity.getCorpid(), firstIndexTypeEnum, biProWarehouseEntity.getBtfieldType(),
                                    biProWarehouseEntity.getBtlinkAttr(),biProWarehouseEntity.getBtinData(),biProWarehouseEntity.getBtbusinessType(),
                                    biProWarehouseEntity.getBtsaasMark(),biProWarehouseEntity.getBtformId());
                    //获取从表数据
                    slaveList(slaveListDTO);
                    if(hasSecondSlave){
                        IndexTypeEnum secondIndexTypeEnum = indexTypeModel.getIndexType(biProWarehouseEntity.getCorpid(), biProWarehouseEntity.getCtbusinessType(), biProWarehouseEntity.getCtsaasMark());
                        SlaveListDTO secondSlaveListDTO =
                                new SlaveListDTO(cMap, aSecondFilterSet, biProWarehouseEntity.getCorpid(), secondIndexTypeEnum, biProWarehouseEntity.getCtfieldType(),
                                        biProWarehouseEntity.getCtlinkAttr(),biProWarehouseEntity.getCtinData(),biProWarehouseEntity.getCtbusinessType(),
                                        biProWarehouseEntity.getCtsaasMark(),biProWarehouseEntity.getCtformId());
                        //获取从表数据
                        slaveList(secondSlaveListDTO);
                    }
                    //拼装数据
                    List<InsertDTO> insertList = new ArrayList<>();
                    List<StatisticPaasFormDataEsEntity> structureInsertList = structureInsertList(biProWarehouseEntity.getCorpid(), hasSecondSlave, biProWarehouseEntity.getAtlinkAttr(), biRelTableEnum, null,
                            biProWarehouseEntity.getId(), FormLocationEnum.B.getCode(), driverList, biProWarehouseEntity.getAtinData(), bMap, cMap, insertList,explainMap,biProWarehouseEntity.getAtfieldType(),biProWarehouseEntity.getAtSecondLinkAttr(),biProWarehouseEntity.getAtSecondFieldType(),biProWarehouseEntity.getAtSecondInData());
                    //TODO
                    String dir = ProBaseConfig.getProjectName();
                    if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                        paasEsModel.insertBatch(insertList,WriteRequest.RefreshPolicy.NONE,false);
                    }else {
                        initMessageQueueHelp.initMessage(structureInsertList, biRelTableEnum.getIndex());
                    }
                    initAllTeamData(hasSecondSlave, biProWarehouseEntity.getAtbusinessType(), biProWarehouseEntity.getAtsaasMark(), biRelTableEnum, biProWarehouseEntity, structureInsertList);
                } catch (Exception e) {
                    LOG.error("",e);
                }
            }
        }
    }



    private void initTeamData(Integer businessType, Integer saasMark, BiRelTableEnum biRelTableEnum, Long warehouseId,  Map<Long, Set<String>> teamLinkMap,FormLocationEnum formLocationEnum) throws XbbException {
        if(!teamLinkMap.isEmpty() && SaasMarkEnum.SAAS.getCode().equals(saasMark)){
            BITeamEnum bITeamEnum = BITeamEnum.getByParentCode(businessType);
            if(bITeamEnum !=null){
                XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(bITeamEnum.getCode());
                Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                params.put("orderByStr", "id");
                params.put("dataIdIn", teamLinkMap.keySet());
                boolean teamFlag = true;
                while (teamFlag) {
                    List teamList = null;
                    teamList = teamHelp.getList(xbbRefTypeEnum, params, teamList);
                    if (teamList == null || teamList.isEmpty()) {
                        teamFlag = false;
                    } else {
                        //大于上次最大的id
                        teamHelp.setIdGte(xbbRefTypeEnum, params, teamList);
                        List<InsertDTO> insertList = new ArrayList<>();
                        for (Object object : teamList) {
                            UserTeamEntity userTeamEntity = (UserTeamEntity)object;
                            //设置parentId
                            Set<String> parentIdSet = teamLinkMap.get(userTeamEntity.getDataId());
                            if(parentIdSet == null){
                                continue;
                            }
                            for (String parentId : parentIdSet) {
                                InsertDTO insertDTO = new InsertDTO();
                                insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
                                //
                                teamHelp.setInsertDTO(xbbRefTypeEnum, object, insertDTO);
                                insertDTO.setEsId(insertDTO.getEsId() + "_" + parentId);
                                insertDTO.setParent(parentId);
                                BiUserTeamEntity biUserTeamEntity = new BiUserTeamEntity();
                                BeanUtil.copyProperties(userTeamEntity,biUserTeamEntity);
                                biUserTeamEntity.setParentId(parentId);
                                biUserTeamEntity.setWarehouseId(warehouseId);
                                insertDTO.setSource(BeanUtil.convertBean2Map(biUserTeamEntity, false, true));
                                insertDTO.setIndex(biRelTableEnum.getIndex());
                                insertDTO.setType(formLocationEnum.getLocation()+ "my_team");
                                insertList.add(insertDTO);
                            }
                        }
                        paasEsModel.insertBatch(insertList,WriteRequest.RefreshPolicy.NONE,false);
                    }
                }
            }
        }
    }

    /**
     * 初始化数据
     * @param corpid
     * @param hasSecondSlave
     * @param driverSourcePojo
     * @param slaveSourceList
     * @param businessType
     * @param saasMark
     * @param biRelTableEnum
     * @param slaveSourceListForCorrelation
     * @param biProWarehouseEntity
     * @throws XbbException
     */
    private void sendData(String corpid, boolean hasSecondSlave, SlaveSourcePojo driverSourcePojo, List<SlaveSourcePojo> slaveSourceList, Integer businessType, Integer saasMark, BiRelTableEnum biRelTableEnum, List<SlaveSourcePojo> slaveSourceListForCorrelation, BiProWarehouseEntity biProWarehouseEntity) throws XbbException {
        Integer bLocation = slaveSourceListForCorrelation.get(1).getLocation();
        //提前获取，避免循环内重复获取
        PaasFormDataEsMappingDTO paasFormDataEsMappingDTO = new PaasFormDataEsMappingDTO();
        BeanUtil.copyProperties(slaveSourceList.get(0),paasFormDataEsMappingDTO);
        SlaveSourcePojo slaveSourcePojo = slaveSourceList.get(0);
        IndexTypeEnum firstIndexTypeEnum = indexTypeModel.getIndexType(corpid, slaveSourcePojo.getBusinessType(), slaveSourcePojo.getSaasMark());
        IndexTypeEnum secondIndexTypeEnum = null;
        if(hasSecondSlave){
            secondIndexTypeEnum = indexTypeModel.getIndexType(corpid, slaveSourceList.get(1).getBusinessType(), slaveSourceList.get(1).getSaasMark());
        }

        Map<String, Object> map =  BeanUtil.convertBean2Map(driverSourcePojo,true);
        map.put("corpid", corpid);
        // mapper.xml中统一limit改为pageSize
        map.put("pageSize", SizeConstant.PAGE_SIZE);
        //按id排序
        map.put("orderByStr", "id");
        List<PaasFormDataEntityExt>  driverList;
        Integer driverInData = driverSourcePojo.getInData();
        Integer driverSecondInData = driverSourcePojo.getSecondInData();
        /***==============================获取解释====================================***/
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Integer fieldType = driverSourcePojo.getFieldType();
        Integer secondFieldType = driverSourcePojo.getSecondFieldType();
        if(FieldTypeEnum.getExplainFieldTypeEnumType().contains(fieldType) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(secondFieldType)){
            Long formId = driverSourcePojo.getFormId();
            PaasFormEntityExt paasFormEntityExt = paasFormModel.getByKey(formId, corpid);
            if (paasFormEntityExt == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            //解释表信息
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(formId, corpid);
            if (paasFormExplainEntity == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
            List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
            if(!explainList.isEmpty()){
                //把循环内的判断提出来
                if(SaasMarkEnum.SAAS.getCode().equals(driverSourcePojo.getSaasMark())) {
                    for (FieldAttrEntity fieldAttrEntity : explainList) {
                        String attr = fieldAttrEntity.getAttr();
                        List<ItemPoJo> item = proFormHelp.getItem(fieldAttrEntity, driverSourcePojo.getBusinessType(), corpid, driverSourcePojo.getFormId());
                        fieldAttrEntity.setItems(item);
                        explainMap.put(attr, fieldAttrEntity);
                    }
                }else{
                    for (FieldAttrEntity fieldAttrEntity : explainList) {
                        String attr = fieldAttrEntity.getAttr();
                        explainMap.put(attr, fieldAttrEntity);
                    }
                }
            }
        }
        /***==============================获取解释====================================***/
        Set<String> instockTypeEnumCodeSet = new HashSet<>();
        Set<String> secondInstockTypeEnumCodeSet = new HashSet<>();
        OutstockTypeEnum outstockTypeEnum = null;
        OutstockTypeEnum secondOutstockTypeEnum = null;
        /*
         * 入库单+ 采购合同    采购入库
         * 入库单+生产单   生产成品入库、退料入库
         *
         * 出库单+合同订单   销售出库
         * 出库单+采购退货单  采购退货出库
         * 出库单+ 生产单   生产物料出库
         * */
        if(Objects.equals(XbbRefTypeEnum.INSTOCK.getCode(),businessType)
                ||Objects.equals(XbbRefTypeEnum.PURCHASE_INSTOCK.getCode(),businessType)
                || Objects.equals(XbbRefTypeEnum.PRODUCTION_INSTOCK.getCode(),businessType)
                ||Objects.equals(XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK.getCode(),businessType)){
            //TODO 后面是否可以通过枚举的businessType比较
            if(Objects.equals(XbbRefTypeEnum.PURCHASE.getCode(),slaveSourceList.get(0).getBusinessType())){
                instockTypeEnumCodeSet.add(String.valueOf(InstockTypeEnum.PURCHASE_INSTOCK.getCode()));
            }else if(Objects.equals(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(),slaveSourceList.get(0).getBusinessType())){
                instockTypeEnumCodeSet.add(String.valueOf( InstockTypeEnum.FINSHED_PRODUCT_INSTOCK.getCode()));
                instockTypeEnumCodeSet.add(String.valueOf( InstockTypeEnum.RETURNED_MATERIEL_INSTOCK.getCode()));
            }
            if(hasSecondSlave){
                if(Objects.equals(XbbRefTypeEnum.PURCHASE.getCode(),slaveSourceList.get(1).getBusinessType())){
                    secondInstockTypeEnumCodeSet.add(String.valueOf(InstockTypeEnum.PURCHASE_INSTOCK.getCode()));
                }else if(Objects.equals(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(),slaveSourceList.get(1).getBusinessType())){
                    secondInstockTypeEnumCodeSet.add(String.valueOf(InstockTypeEnum.FINSHED_PRODUCT_INSTOCK.getCode()));
                    secondInstockTypeEnumCodeSet.add(String.valueOf(InstockTypeEnum.RETURNED_MATERIEL_INSTOCK.getCode()));
                }
            }
        }else if(Objects.equals(XbbRefTypeEnum.OUTSTOCK.getCode(),businessType)){
            if(Objects.equals(XbbRefTypeEnum.CONTRACT.getCode(),slaveSourceList.get(0).getBusinessType())){
                outstockTypeEnum = OutstockTypeEnum.CONTRACT_OUTSTOCK;
            }else if(Objects.equals(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(),slaveSourceList.get(0).getBusinessType())){
                outstockTypeEnum = OutstockTypeEnum.MATERIEL_OUTSTOCK;
            }
            if(hasSecondSlave){
                if(Objects.equals(XbbRefTypeEnum.CONTRACT.getCode(),slaveSourceList.get(1).getBusinessType())){
                    secondOutstockTypeEnum = OutstockTypeEnum.CONTRACT_OUTSTOCK;
                }else if(Objects.equals(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(),slaveSourceList.get(1).getBusinessType())){
                    secondOutstockTypeEnum = OutstockTypeEnum.MATERIEL_OUTSTOCK;
                }
            }
        }
        boolean flag = true;
        while (flag){
            driverList  = findEntitys(map, businessType, saasMark, null);
            if (driverList == null || driverList.isEmpty()) {
                flag = false;
            } else {
                //TODO 暂时提交
                map.put("idGte",driverList.get(driverList.size() - 1).getId());
                Set<Object> aFilterSet = new HashSet<>();
                //主表的第二个关联字段
                Set<Object> aSecondFilterSet = new HashSet<>();
                for (PaasFormDataEntityExt dataEntityExt : driverList) {
                    dataEntityExt.setDataId(dataEntityExt.getId());
                    if(StockConstant.BI_INSTOCK_TYPE_LIST.contains(businessType) && instockTypeEnumCodeSet !=null && !instockTypeEnumCodeSet.isEmpty()){
                        //出库单
                        if(instockTypeEnumCodeSet.contains(dataEntityExt.getData().getString(InstockEnum.TYPE.getAttr()))){
                            setAFilterSet(driverInData, explainMap, fieldType, aFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrCopy());
                        }
                    }else if(StockConstant.BI_OUTSTOCK_TYPE_LIST.contains(businessType)  && outstockTypeEnum !=null){
                        if(Objects.equals(String.valueOf(outstockTypeEnum.getCode()),dataEntityExt.getData().getString(OutstockEnum.TYPE.getAttr()))){
                            setAFilterSet(driverInData, explainMap, fieldType, aFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrCopy());
                        }
                    } else {
                        //正常的
                        setAFilterSet(driverInData, explainMap, fieldType, aFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrCopy());
                    }
                    if(hasSecondSlave){
                        if(StockConstant.BI_INSTOCK_TYPE_LIST.contains(businessType)  && secondInstockTypeEnumCodeSet !=null && !secondInstockTypeEnumCodeSet.isEmpty()){
                            if(secondInstockTypeEnumCodeSet.contains(dataEntityExt.getData().getString(InstockEnum.TYPE.getAttr()))){
                                setAFilterSet(driverSecondInData, explainMap, secondFieldType, aSecondFilterSet, dataEntityExt, driverSourcePojo.getSecondLinkAttrCopy());
                            }
                        }else if(StockConstant.BI_OUTSTOCK_TYPE_LIST.contains(businessType)  && secondOutstockTypeEnum !=null){
                            if(Objects.equals(String.valueOf(secondOutstockTypeEnum.getCode()),dataEntityExt.getData().getString(OutstockEnum.TYPE.getAttr()))){
                                setAFilterSet(driverSecondInData, explainMap, secondFieldType, aSecondFilterSet, dataEntityExt, driverSourcePojo.getSecondLinkAttrCopy());
                            }
                        } else {
                            setAFilterSet(driverSecondInData, explainMap, secondFieldType, aSecondFilterSet, dataEntityExt, driverSourcePojo.getSecondLinkAttrCopy());
                        }
                    }
                }
                Map<Object,PaasFormDataEntityExt> bMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Object,PaasFormDataEntityExt> cMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                SlaveListDTO slaveListDTO = new SlaveListDTO();
                BeanUtils.copyProperties(slaveSourceList.get(0),slaveListDTO);
                slaveListDTO.setCorpid(corpid);
                slaveListDTO.setSlaveSet(aFilterSet);
                slaveListDTO.setSlavesMap(bMap);
                slaveListDTO.setIndexTypeEnum(firstIndexTypeEnum);
                slaveListDTO.setWarehouseId(biProWarehouseEntity.getId());
                slaveListDTO.setBiRelTableEnum(biRelTableEnum);
                //获取从表数据
                slaveList(slaveListDTO);
                //第二张从属表
                if(hasSecondSlave){
                    SlaveListDTO secondSlaveListDTO = new SlaveListDTO();
                    BeanUtils.copyProperties(slaveSourceList.get(1),secondSlaveListDTO);
                    secondSlaveListDTO.setCorpid(corpid);
                    secondSlaveListDTO.setSlaveSet(aSecondFilterSet);
                    secondSlaveListDTO.setSlavesMap(cMap);
                    secondSlaveListDTO.setIndexTypeEnum(secondIndexTypeEnum);
                    slaveListDTO.setWarehouseId(biProWarehouseEntity.getId());
                    slaveListDTO.setBiRelTableEnum(biRelTableEnum);
                    //获取从表数据
                    slaveList(secondSlaveListDTO);
                }
                //拼装数据
                List<InsertDTO> insertList = new ArrayList<>();
                List<StatisticPaasFormDataEsEntity> structureInsertList = structureInsertList(corpid, hasSecondSlave, driverSourcePojo.getLinkAttrCopy(), biRelTableEnum, slaveSourceListForCorrelation, biProWarehouseEntity.getId(), bLocation, driverList, driverInData, bMap, cMap, insertList, explainMap, fieldType, driverSourcePojo.getSecondLinkAttrCopy(), secondFieldType, driverSecondInData);
                String dir = ProBaseConfig.getProjectName();
                // todo 测试二缺少自定义统计集群环境，暂时用本地的 || Objects.equals(dir, ProVersionEnum.TEST2.getAlias())
                if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                    paasEsModel.insertBatch(insertList,WriteRequest.RefreshPolicy.NONE,false);
                }else {
                    initMessageQueueHelp.initMessage(structureInsertList, biRelTableEnum.getIndex());
                }
                //团队的处理
                initAllTeamData(hasSecondSlave, businessType, saasMark, biRelTableEnum, biProWarehouseEntity, structureInsertList);
            }

        }
    }

    /**
     *
     * @param corpid
     * @param hasSecondSlave
     * @param driverSourcePojo  主表
     * @param slaveSourceList
     * @param businessType
     * @param saasMark
     * @param biRelTableEnum
     * @param slaveSourceListForCorrelation
     * @param biProWareEntity
     * @param chartEntity
     * @throws XbbException
     */
    private void sendbBiData(String corpid, boolean hasSecondSlave, SlaveSourcePojo driverSourcePojo, List<SlaveSourcePojo> slaveSourceList, Integer businessType, Integer saasMark, BiRelTableEnum biRelTableEnum, List<SlaveSourcePojo> slaveSourceListForCorrelation, BiProWareEntity biProWareEntity, ChartEntity chartEntity) throws Exception {
        //提前获取，避免循环内重复获取
        Long biProWareEntityId = biProWareEntity.getId();
        PaasFormDataEsMappingDTO paasFormDataEsMappingDTO = new PaasFormDataEsMappingDTO();
        IndexTypeEnum firstIndexTypeEnum = null;
        if(CollectionsUtil.isNotEmpty(slaveSourceList)){
            firstIndexTypeEnum = indexTypeModel.getIndexType(corpid, slaveSourceList.get(0).getBusinessType(), slaveSourceList.get(0).getSaasMark());
        }
        IndexTypeEnum secondIndexTypeEnum = null;
        IndexTypeEnum indexTypeEnumC = null;
        IndexTypeEnum indexTypeEnumD = null;
        IndexTypeEnum indexTypeEnumE = null;
        if(hasSecondSlave){
            secondIndexTypeEnum = indexTypeModel.getIndexType(corpid, slaveSourceList.get(1).getBusinessType(), slaveSourceList.get(1).getSaasMark());
        }
        if(slaveSourceList.size() > 2){
            indexTypeEnumC = getIndexType(slaveSourceList.get(2), corpid);
        }
        if(slaveSourceList.size() > 3){
            indexTypeEnumD = getIndexType(slaveSourceList.get(3), corpid);
        }
        if(slaveSourceList.size() > 4){
            indexTypeEnumE = getIndexType(slaveSourceList.get(4), corpid);
        }

        Map<String, Object> map =  BeanUtil.convertBean2Map(driverSourcePojo,true);
        map.put("corpid", corpid);
        // mapper.xml中统一limit改为pageSize
        map.put("pageSize", SizeConstant.PAGE_SIZE);
        //按id排序
        map.put("orderByStr", "id");
        PaasFormRefEntity paasFormRefEntity = null;
        if(CollectionsUtil.isNotEmpty(slaveSourceList) && BIProductReformEnum.getProductSubBusList().contains(businessType)){
            paasFormRefEntity = paasFormRefModel.getMainFormIdByFormId(driverSourcePojo.getFormId(), corpid);
            if (Objects.isNull(paasFormRefEntity)) {
                throw new XbbException(BiErrorCodeEnum.API_ERROR_241022);
            }
            Long mainFormId = paasFormRefEntity.getMainFormId();
            map.put("formId",mainFormId);
        }
        List<PaasFormDataEntityExt>  driverList;
        Integer driverInData = driverSourcePojo.getInData();
        Integer driverSecondInData = driverSourcePojo.getSecondInData();
        /***==============================获取解释====================================***/
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Integer fieldType = driverSourcePojo.getFieldType();
        Integer secondFieldType = driverSourcePojo.getSecondFieldType();
        if(FieldTypeEnum.getExplainFieldTypeEnumType().contains(fieldType) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(secondFieldType) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(driverSourcePojo.getLinkAttrCFieldType()) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(driverSourcePojo.getLinkAttrDFieldType()) || FieldTypeEnum.getExplainFieldTypeEnumType().contains(driverSourcePojo.getLinkAttrEFieldType())){
            Long formId = driverSourcePojo.getFormId();
            PaasFormEntityExt paasFormEntityExt = paasFormModel.getByKey(formId, corpid);
            if (paasFormEntityExt == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            //解释表信息
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(formId, corpid);
            if (paasFormExplainEntity == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
            List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
            if(!explainList.isEmpty()){
                if(SaasMarkEnum.SAAS.getCode().equals(driverSourcePojo.getSaasMark())) {
                    for (FieldAttrEntity fieldAttrEntity : explainList) {
                        String attr = fieldAttrEntity.getAttr();
                        List<ItemPoJo> item = proFormHelp.getItem(fieldAttrEntity, driverSourcePojo.getBusinessType(), corpid, driverSourcePojo.getFormId());
                        fieldAttrEntity.setItems(item);
                        explainMap.put(attr, fieldAttrEntity);
                    }
                }else {
                    for (FieldAttrEntity fieldAttrEntity : explainList) {
                        String attr = fieldAttrEntity.getAttr();
                        explainMap.put(attr, fieldAttrEntity);
                    }
                }
            }
        }
        /***==============================获取解释====================================***/
        boolean flag = true;
        while (flag){
            driverList  = findEntitys(map, businessType, saasMark, paasFormRefEntity);
            if (driverList == null || driverList.isEmpty()) {
                flag = false;
            } else {
                //TODO 暂时提交
                map.put("idGte",driverList.get(driverList.size() - 1).getId());
                Set<Object> aFilterSet = new HashSet<>();
                //主表的第二个关联字段
                Set<Object> aSecondFilterSet = new HashSet<>();
                Set<Object> linkCFilterSet = new HashSet<>();
                Set<Object> linkDFilterSet = new HashSet<>();
                Set<Object> linkEFilterSet = new HashSet<>();
                Set<Long> dataIdSet = new HashSet<>();
                for (PaasFormDataEntityExt dataEntityExt : driverList) {
                    Long id = dataEntityExt.getId();
                    dataEntityExt.setDataId(id);
                    dataIdSet.add(id);
                    if(firstIndexTypeEnum != null){
                        setAFilterSet(driverInData, explainMap, fieldType, aFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrCopy());
                    }
                    if(hasSecondSlave){
                        setAFilterSet(driverSecondInData, explainMap, secondFieldType, aSecondFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrBCopy());
                    }
                    if(indexTypeEnumC != null){
                        setAFilterSet(driverSourcePojo.getLinkAttrCInData(), explainMap, driverSourcePojo.getLinkAttrCFieldType(), linkCFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrCCopy());
                    }
                    if(indexTypeEnumD != null){
                        setAFilterSet(driverSourcePojo.getLinkAttrDInData(), explainMap, driverSourcePojo.getLinkAttrDFieldType(), linkDFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrDCopy());
                    }
                    if(indexTypeEnumE != null){
                        setAFilterSet(driverSourcePojo.getLinkAttrEInData(), explainMap, driverSourcePojo.getLinkAttrEFieldType(), linkEFilterSet, dataEntityExt, driverSourcePojo.getLinkAttrECopy());
                    }
                }
                Map<Object,PaasFormDataEntityExt> bMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Object,PaasFormDataEntityExt> cMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Object,PaasFormDataEntityExt> dMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Object,PaasFormDataEntityExt> eMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Object,PaasFormDataEntityExt> fMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                //团队的处理
                Map<Long,Set<UserTeamEntity>> aUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Long,Set<UserTeamEntity>> bUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Long,Set<UserTeamEntity>> cUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Long,Set<UserTeamEntity>> dUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Long,Set<UserTeamEntity>> eUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Map<Long,Set<UserTeamEntity>> fUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

                //团队数据的处理
                if( SaasMarkEnum.SAAS.getCode().equals(saasMark)){
                    BITeamEnum bITeamEnum = BITeamEnum.getByParentCode(businessType);
                    if(bITeamEnum !=null){
                        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(bITeamEnum.getCode());
                        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        params.put("orderByStr", "id");
                        params.put("dataIdIn", dataIdSet);
                        params.put("corpid", corpid);
                        params.put("pageSize", SizeConstant.PAGE_SIZE);
                        boolean teamFlag = true;
                        while (teamFlag) {
                            List teamList = null;
                            teamList = teamHelp.getList(xbbRefTypeEnum, params, teamList);
                            if (teamList == null || teamList.isEmpty()) {
                                teamFlag = false;
                            } else {
                                //大于上次最大的id
                                teamHelp.setIdGte(xbbRefTypeEnum, params, teamList);
                                for (Object object : teamList) {
                                    UserTeamEntity userTeamEntity = (UserTeamEntity)object;
                                    Long dataId = userTeamEntity.getDataId();
                                    Set<UserTeamEntity> userTeamEntitySet;
                                    if(aUserMap.containsKey(dataId)){
                                        userTeamEntitySet = aUserMap.get(dataId);
                                        if(userTeamEntitySet == null){
                                            userTeamEntitySet = new HashSet<>();
                                        }
                                    }else {
                                        userTeamEntitySet = new HashSet<>();
                                    }
                                    userTeamEntitySet.add(userTeamEntity);
                                    aUserMap.put(dataId,userTeamEntitySet);
                                }
                            }
                        }
                    }
                }
                //
                if(firstIndexTypeEnum !=null){
                    SlaveListDTO slaveListDTO = new SlaveListDTO();
                    BeanUtils.copyProperties(slaveSourceList.get(0),slaveListDTO);
                    slaveListDTO.setCorpid(corpid);
                    slaveListDTO.setSlaveSet(aFilterSet);
                    slaveListDTO.setSlavesMap(bMap);
                    slaveListDTO.setSlavesUserMap(bUserMap);
                    slaveListDTO.setIndexTypeEnum(firstIndexTypeEnum);
                    slaveListDTO.setWarehouseId(biProWareEntityId);
                    slaveListDTO.setBiRelTableEnum(biRelTableEnum);
                    slaveListDTO.setChartEntity(chartEntity);
                    slaveListDTO.setDriverBusinessType(businessType);
                    //获取从表数据
                    slaveList(slaveListDTO);
                }
                //第二张从属表
                if(hasSecondSlave){
                    SlaveListDTO secondSlaveListDTO = new SlaveListDTO();
                    BeanUtils.copyProperties(slaveSourceList.get(1),secondSlaveListDTO);
                    secondSlaveListDTO.setCorpid(corpid);
                    secondSlaveListDTO.setSlaveSet(aSecondFilterSet);
                    secondSlaveListDTO.setSlavesMap(cMap);
                    secondSlaveListDTO.setSlavesUserMap(cUserMap);
                    secondSlaveListDTO.setIndexTypeEnum(secondIndexTypeEnum);
                    secondSlaveListDTO.setWarehouseId(biProWareEntityId);
                    secondSlaveListDTO.setBiRelTableEnum(biRelTableEnum);
                    secondSlaveListDTO.setChartEntity(chartEntity);
                    //获取从表数据
                    slaveList(secondSlaveListDTO);
                }
                if (indexTypeEnumC != null) {
                    SlaveListDTO slistDTO = new SlaveListDTO();
                    BeanUtils.copyProperties(slaveSourceList.get(2),slistDTO);
                    slistDTO.setCorpid(corpid);
                    slistDTO.setSlaveSet(linkCFilterSet);
                    slistDTO.setSlavesMap(dMap);
                    slistDTO.setSlavesUserMap(dUserMap);
                    slistDTO.setIndexTypeEnum(indexTypeEnumC);
                    slistDTO.setWarehouseId(biProWareEntityId);
                    slistDTO.setBiRelTableEnum(biRelTableEnum);
                    slistDTO.setChartEntity(chartEntity);
                    //获取从表数据
                    slaveList(slistDTO);
                }
                if(indexTypeEnumD != null ){
                    SlaveListDTO slistDTO = new SlaveListDTO();
                    BeanUtils.copyProperties(slaveSourceList.get(3),slistDTO);
                    slistDTO.setCorpid(corpid);
                    slistDTO.setSlaveSet(linkDFilterSet);
                    slistDTO.setSlavesMap(eMap);
                    slistDTO.setSlavesUserMap(eUserMap);
                    slistDTO.setIndexTypeEnum(indexTypeEnumD);
                    slistDTO.setWarehouseId(biProWareEntityId);
                    slistDTO.setBiRelTableEnum(biRelTableEnum);
                    slistDTO.setChartEntity(chartEntity);
                    //获取从表数据
                    slaveList(slistDTO);
                }
                if(indexTypeEnumE != null ){
                    SlaveListDTO slistDTO = new SlaveListDTO();
                    BeanUtils.copyProperties(slaveSourceList.get(4),slistDTO);
                    slistDTO.setCorpid(corpid);
                    slistDTO.setSlaveSet(linkEFilterSet);
                    slistDTO.setSlavesMap(fMap);
                    slistDTO.setSlavesUserMap(fUserMap);
                    slistDTO.setIndexTypeEnum(indexTypeEnumE);
                    slistDTO.setWarehouseId(biProWareEntityId);
                    slistDTO.setBiRelTableEnum(biRelTableEnum);
                    slistDTO.setChartEntity(chartEntity);
                    //获取从表数据
                    slaveList(slistDTO);
                }
                //拼装数据
                List<InsertDTO> insertList = new ArrayList<>();
                StructureBiInsertDTO structureBiInsertDTO = new StructureBiInsertDTO(corpid, hasSecondSlave, driverSourcePojo.getLinkAttrCopy(), biRelTableEnum, slaveSourceListForCorrelation, biProWareEntityId,
                         driverList, driverInData,bMap, cMap, dMap, eMap, fMap,  insertList, explainMap, fieldType, driverSourcePojo.getLinkAttrBCopy(), secondFieldType, driverSecondInData, chartEntity.getFormIdAttr(),
                        driverSourcePojo.getLinkAttrC(),driverSourcePojo.getLinkAttrCInData(),driverSourcePojo.getLinkAttrCFieldType(),
                        driverSourcePojo.getLinkAttrD(),driverSourcePojo.getLinkAttrDInData(),driverSourcePojo.getLinkAttrDFieldType(),
                        driverSourcePojo.getLinkAttrE(),driverSourcePojo.getLinkAttrEInData(),driverSourcePojo.getLinkAttrEFieldType(),driverSourcePojo.getBusinessType(),driverSourcePojo.getFormId()
                );
                structureBiInsertDTO.setAUserMap(aUserMap);
                structureBiInsertDTO.setBUserMap(bUserMap);
                structureBiInsertDTO.setCUserMap(cUserMap);
                structureBiInsertDTO.setDUserMap(dUserMap);
                structureBiInsertDTO.setEUserMap(eUserMap);
                structureBiInsertDTO.setFUserMap(fUserMap);
                List<StatisticBiFormDataEntity> structureInsertList = structureBiInsertList(structureBiInsertDTO);
                String dir = ProBaseConfig.getProjectName();
                if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                    paasEsModel.insertBatch(insertList,WriteRequest.RefreshPolicy.NONE,false,false);
                }else {
                    initMessageQueueHelp.initBiMessage(structureInsertList, biRelTableEnum.getIndex());
                    structureInsertList.clear();
                }
                //子表单数据的处理
                IndexTypeEnum indexType = indexTypeModel.getIndexType(corpid, businessType, saasMark);
                structureSubData(corpid, indexType, biRelTableEnum, biProWareEntityId, dataIdSet, chartEntity.getFormIdAttr(),null);
                if (firstIndexTypeEnum != null) {
                    structureSubData(corpid, firstIndexTypeEnum, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveBIdMap().keySet(), chartEntity.getFormIdAttr(), structureBiInsertDTO.getSlaveBIdMap());
                }
                if (secondIndexTypeEnum != null) {
                    structureSubData(corpid, secondIndexTypeEnum, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveCIdMap().keySet(), chartEntity.getFormIdAttr(), structureBiInsertDTO.getSlaveCIdMap());
                }
                if (indexTypeEnumC != null) {
                    structureSubData(corpid, indexTypeEnumC, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveDIdMap().keySet(), chartEntity.getFormIdAttr(), structureBiInsertDTO.getSlaveDIdMap());
                }
                if (indexTypeEnumD != null) {
                    structureSubData(corpid, indexTypeEnumD, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveEIdMap().keySet(), chartEntity.getFormIdAttr(), structureBiInsertDTO.getSlaveEIdMap());
                }
                if (indexTypeEnumE != null) {
                    structureSubData(corpid, indexTypeEnumE, biRelTableEnum, biProWareEntityId, structureBiInsertDTO.getSlaveFIdMap().keySet(), chartEntity.getFormIdAttr(), structureBiInsertDTO.getSlaveFIdMap());
                }
                //手动清除内层集合的数据
                cleanS(structureBiInsertDTO);
            }
        }
        LOG.info("======success=========="+corpid);
    }


    /**
     * 这边需要兼容一下产品子表单一些特殊场景。比如退货退款产品存的是合同id,所以要把合同id转化成退货退款id。
     * 为啥要写在第一张从表这边,因为主表选了产品子表，多表的话，第一张从表的业务是定死的。
     *
     * @param businessType 主表的业务类型
     * @param aFilterSet 第一张从表的主键id
     * @param firstIndexTypeEnum 第一张从表的ES索引
     * @param slaveListDTO
     * @param oldAndNewKeyMap 老的和新的key对应的map
     */
    private Set<Object> handSpecialSubProduct(Integer businessType, Set<Object> aFilterSet, IndexTypeEnum firstIndexTypeEnum,
                                              SlaveListDTO slaveListDTO, Map<Object, Object> oldAndNewKeyMap) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        //代表主表是产品（退货退款），第一张从表是退货退款
        if (Objects.equals(XbbRefTypeEnum.CONTRACT_PRODUCT.getCode(), businessType) && Objects.equals(firstIndexTypeEnum, IndexTypeEnum.IDX_SAAS_REFUND)) {
            boolQueryBuilder
                    .filter(termQuery("corpid.keyword", slaveListDTO.getCorpid()))
                    .filter(termsQuery(RefundEnum.getEsAttr4Keyword(RefundEnum.RED_CONTRACT_ID), aFilterSet));
            boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,firstIndexTypeEnum.getType()));
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(boolQueryBuilder).fetchSource(new String[]{"dataId","corpid","data"}, null);
            SearchRequest searchRequest = new SearchRequest(firstIndexTypeEnum.getIndex());
            EsUtil.setPage(sourceBuilder, 1, EsNormalConstant.FIND_NUM);
            searchRequest.source(sourceBuilder);
            //这里只有一页，因为外面做了限定
            List<PaasFormDataEntityExt> content  = xbbElasticsearchRestTemplate.findScrollList(PaasFormDataEntityExt.class,searchRequest,SizeConstant.SCROLL_TIME_IN_MILLIS);
            Set<Object> newAFilterSet = new HashSet<>();
            if (CollectionsUtil.isNotEmpty(content)) {
                for (PaasFormDataEntityExt paasFormDataEntityExt : content) {
                    oldAndNewKeyMap.put(paasFormDataEntityExt.getDataId(), Long.parseLong(String.valueOf(paasFormDataEntityExt.getData().get(RefundEnum.RED_CONTRACT_ID.getAttr()))));
                    newAFilterSet.add(paasFormDataEntityExt.getDataId());
                }
                return newAFilterSet;
            }
        }
        return null;
    }


    private void cleanS(StructureBiInsertDTO structureBiInsertDTO) {
        if (structureBiInsertDTO.getDriverList() != null) {
            structureBiInsertDTO.getDriverList().clear();
        }
        if(structureBiInsertDTO.getBMap() != null){
            structureBiInsertDTO.getBMap().clear();
        }
        if(structureBiInsertDTO.getCMap() != null){
            structureBiInsertDTO.getCMap().clear();
        }
        if(structureBiInsertDTO.getDMap() != null){
            structureBiInsertDTO.getDMap().clear();
        }
        if(structureBiInsertDTO.getEMap() != null){
            structureBiInsertDTO.getEMap().clear();
        }
        if(structureBiInsertDTO.getFMap() != null){
            structureBiInsertDTO.getFMap().clear();
        }
        if(structureBiInsertDTO.getAUserMap() != null){
            structureBiInsertDTO.getAUserMap().clear();
        }
        if(structureBiInsertDTO.getBUserMap() != null){
            structureBiInsertDTO.getBUserMap().clear();
        }
        if(structureBiInsertDTO.getCUserMap() != null){
            structureBiInsertDTO.getCUserMap().clear();
        }
        if(structureBiInsertDTO.getDUserMap() != null){
            structureBiInsertDTO.getDUserMap().clear();
        }
        if(structureBiInsertDTO.getEUserMap() != null){
            structureBiInsertDTO.getEUserMap().clear();
        }
        if(structureBiInsertDTO.getFUserMap() != null){
            structureBiInsertDTO.getFUserMap().clear();
        }
        if(structureBiInsertDTO.getInsertList() != null){
            structureBiInsertDTO.getInsertList().clear();
        }
        if(structureBiInsertDTO.getExplainMap() != null){
            structureBiInsertDTO.getExplainMap().clear();
        }
        if(structureBiInsertDTO.getSlaveBIdMap() != null){
            structureBiInsertDTO.getSlaveBIdMap().clear();
        }
        if(structureBiInsertDTO.getSlaveCIdMap() != null){
            structureBiInsertDTO.getSlaveCIdMap().clear();
        }
        if(structureBiInsertDTO.getSlaveDIdMap() != null){
            structureBiInsertDTO.getSlaveDIdMap().clear();
        }
        if(structureBiInsertDTO.getSlaveEIdMap() != null){
            structureBiInsertDTO.getSlaveEIdMap().clear();
        }
        if(structureBiInsertDTO.getSlaveFIdMap() != null){
            structureBiInsertDTO.getSlaveFIdMap().clear();
        }
    }
    
    /**
     * 处理子表单数据
     * @param corpid
     * @param indexType
     * @param biRelTableEnum
     * @param biProWareEntityId
     * @param dataIdSet
     * @param formIdAttr
     * @param slaveIdMap
     * @throws XbbException
     */
    private void structureSubData(String corpid, IndexTypeEnum indexType, BiRelTableEnum biRelTableEnum, Long biProWareEntityId, Set<Long> dataIdSet, JSONObject formIdAttr,Map<Long,List<Long>>  slaveIdMap) throws XbbException {
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("orderByStr", "id");
        params.put("dataIdIn", dataIdSet);
        params.put("corpid", corpid);
        params.put("pageSize", SizeConstant.PAGE_SIZE);
        boolean subFlag = true;
        //从同一个表单来的数据，所以取第一条数据的formId即可
        while (subFlag) {
            List<? extends PaasFormSubDataEntity> subList = paasFormSubDataModel.getSubDataList(params, IndexTypeEnum.getSubByIndexEnum(indexType));
            if(subList == null || subList.isEmpty()){
                subFlag = false;
            }else {
                params.put("idGte", subList.get(subList.size()-1).getId());
                JSONObject kvJsonObject = (JSONObject) formIdAttr.get(subList.get(0).getFormId().toString());
                List<StatisticBiFormDataEntity> structureInsertList = new ArrayList<>();
                List<InsertDTO> insertList = new ArrayList<>();
                for (PaasFormSubDataEntity paasFormSubDataEntity : subList) {
                    Long dataId = paasFormSubDataEntity.getDataId();
                    //主表的子表单
                    if(slaveIdMap == null){
                        structureSubData(biRelTableEnum, biProWareEntityId, kvJsonObject, structureInsertList, insertList, paasFormSubDataEntity, dataId);
                    }else {
                        List<Long> slaveBIdList = slaveIdMap.get(dataId);
                        if(CollectionsUtil.isEmpty(slaveBIdList)){
                            continue;
                        }
                        for (Long parentId : slaveBIdList) {
                            structureSubData(biRelTableEnum, biProWareEntityId, kvJsonObject, structureInsertList, insertList, paasFormSubDataEntity, parentId);
                        }
                    }
                }
                String dir = ProBaseConfig.getProjectName();
                if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
                    paasEsModel.insertBatch(insertList,WriteRequest.RefreshPolicy.NONE,false,false);
                }else {
                    initMessageQueueHelp.initBiMessage(structureInsertList, biRelTableEnum.getIndex());
                    structureInsertList.clear();
                }
            }
        }
    }

    private void structureSubData(BiRelTableEnum biRelTableEnum, Long biProWareEntityId, JSONObject kvJsonObject, List<StatisticBiFormDataEntity> structureInsertList, List<InsertDTO> insertList, PaasFormSubDataEntity paasFormSubDataEntity, Long parentId) throws XbbException {
        StatisticBiFormDataEntity statisticBiFormDataEntity = paasFormSubDataEntity.getData().toJavaObject(StatisticBiFormDataEntity.class);
        BeanUtil.copyProperties(paasFormSubDataEntity, statisticBiFormDataEntity);
        statisticBiFormDataEntity.setAttr(kvJsonObject.getString(paasFormSubDataEntity.getAttr()));
        structureInsertList.add(statisticBiFormDataEntity);
        String esId = ElasticConstant.SUB_FORM_TYPE+"_"+paasFormSubDataEntity.getCorpid() + "_" + paasFormSubDataEntity.getId() + "_" + biProWareEntityId;
        String parent = paasFormSubDataEntity.getCorpid() + "_" + parentId + "_" + biProWareEntityId;
        statisticBiFormDataEntity.setEsId(esId);
        statisticBiFormDataEntity.setParent(parent);
        String relations = "{\"name\":\"" + ElasticConstant.SUB_FORM_TYPE + "\",\"parent\":\"" + parent + "\"}";
        statisticBiFormDataEntity.setMy_join_field(JSONObject.parse(relations));
        statisticBiFormDataEntity.setWarehouseId(biProWareEntityId);
        String dir = ProBaseConfig.getProjectName();
        if(Objects.equals(dir, ProVersionEnum.LOCAL.getAlias())){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(esId);
            insertDTO.setParent(parent);
            Map<String, Object> source = BeanUtil.convertBean2Map(statisticBiFormDataEntity);
            source.put(ElasticConstant.MY_JOIN_FIELD, JSONObject.parse(relations));
            insertDTO.setSource(source);
            insertDTO.setIndex(biRelTableEnum.getIndex());
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertList.add(insertDTO);
        }
    }

    private IndexTypeEnum getIndexType(SlaveSourcePojo  slaveSource, String corpid ) throws XbbException {
        return indexTypeModel.getIndexType(corpid, slaveSource.getBusinessType(), slaveSource.getSaasMark());
    }

    @Override
    public PaasFormDataEsInitVO init(PaasFormDataEsInitDTO paasFormDataEsInitDTO) throws XbbException {
        PaasFormDataEsInitVO paasFormDataEsInitVO = new PaasFormDataEsInitVO();
        LOG.info("==========1========="+1);
        try {
            Integer byCorpid = paasFormDataEsInitDTO.getByCorpid();
            if(byCorpid != null && Objects.equals(0,byCorpid)){
                paasFormDataEsInitDTO.setCorpid(null);
            }
            Integer pageSize = paasFormDataEsInitDTO.getPageSize();
            if (pageSize == null) {
                paasFormDataEsInitDTO.setPageSize(SizeConstant.PAGE_SIZE);
            }
            BiRelTableEnum   biRelTableEnum = null;
            try {
                biRelTableEnum = BiRelTableEnum.getByRefCode(paasFormDataEsInitDTO.getBusinessType());
            } catch (Exception e) {
                biRelTableEnum = BiRelTableEnum.IDX_STATISTIC_PASS_DATA;
            }
            LOG.info("==========2========="+biRelTableEnum.getIndex());
            Map<String, Object> params = BeanUtil.convertBean2Map(paasFormDataEsInitDTO, true,true);
            params.put("orderByStr", "id");
            boolean flag = true;
            Random r = new Random();
            while (flag) {
                List list = initDataHelp.getList(paasFormDataEsInitDTO.getBusinessType(),paasFormDataEsInitDTO.getSaasMark() ,params, null, null);
                LOG.info("==========3========="+list.size());
                if (CollectionsUtil.isEmpty(list)) {
                    flag = false;
                } else {
                    //大于上次最大的id
                    if (SaasMarkEnum.PAAS.getCode().equals(paasFormDataEsInitDTO.getSaasMark())) {

                    } else {
                        Object o = list.get(list.size() - 1);
                        PaasFormDataEntity paasFormDataEntity = (PaasFormDataEntity) o;
                        params.put("idGte", paasFormDataEntity.getId());
                        List<StatisticPaasFormDataEsEntity>  structureInsertList =  new ArrayList<>();
                        for (Object object : list) {
                            PaasFormDataEntityExt dataEntityExt = (PaasFormDataEntityExt) object;
                            StatisticPaasFormDataEsEntity statisticPaasFormDataEsEntity = new StatisticPaasFormDataEsEntity();
                            String corpid = dataEntityExt.getCorpid();
                            statisticPaasFormDataEsEntity.setCorpid(corpid);
                            Long aLong = Long.valueOf(r.nextInt(1000000000));
                            statisticPaasFormDataEsEntity.setWarehouseId(aLong);
                            statisticPaasFormDataEsEntity.putAtEntity(dataEntityExt);
                            String esId = corpid+"_"+statisticPaasFormDataEsEntity.getAtdataId()+"_"+ aLong+"_"+aLong;
                            statisticPaasFormDataEsEntity.setEsId(esId);
                            structureInsertList.add(statisticPaasFormDataEsEntity);
                        }

                        initMessageQueueHelp.initMessage(structureInsertList, biRelTableEnum.getIndex());
                    }

                }
            }
        } catch (Exception e) {
            LOG.error("初始化失败", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        LOG.error("初始化成功" + paasFormDataEsInitDTO.getCorpid());
        return paasFormDataEsInitVO;
    }
    @Override
    public PaasFormDataEsInitVO initAll(PaasFormDataEsInitDTO paasFormDataEsInitDTO) throws XbbException {
        PaasFormDataEsInitVO paasFormDataEsInitVO = new PaasFormDataEsInitVO();
        LOG.error("==========coming========="+1);
        try {
            paasFormDataEsInitDTO.setSaasMark(1);
        } catch (Exception e) {
            LOG.error("初始化失败", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        LOG.error("初始化成功" + paasFormDataEsInitDTO.getCorpid());
        return paasFormDataEsInitVO;
    }

    /**
     * 设置关联集合
     * @param driverInData 是否在
     * @param explainMap
     * @param fieldType
     * @param aFilterSet  关联字段的值
     * @param dataEntityExt
     * @param linkAttrCopy
     */
    private void setAFilterSet(Integer driverInData, Map<String, FieldAttrEntity> explainMap, Integer fieldType, Set<Object> aFilterSet, PaasFormDataEntityExt dataEntityExt, String linkAttrCopy) {
        Object key;
        if (Objects.equals(0, driverInData)) {
            key = ReflectHelper.valueGet(dataEntityExt, linkAttrCopy, true);
        } else {
            key = dataEntityExt.getData().get(linkAttrCopy);
        }
        if (key != null) {
            key = refacterLinkValue(explainMap, fieldType, key, linkAttrCopy);
            aFilterSet.add(key);
        }
    }

    /**
     * 初始化所有团队的数据
     * @param hasSecondSlave
     * @param businessType
     * @param saasMark
     * @param biRelTableEnum
     * @param biProWarehouseEntity
     * @param structureInsertList
     * @throws XbbException
     */
    private void initAllTeamData(boolean hasSecondSlave,Integer businessType, Integer saasMark, BiRelTableEnum biRelTableEnum, BiProWarehouseEntity biProWarehouseEntity, List<StatisticPaasFormDataEsEntity> structureInsertList) throws XbbException {
        Map<Long, Set<String>> aTeamLinkMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<String>> bTeamLinkMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<String>> cTeamLinkMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (StatisticPaasFormDataEsEntity statisticPaasFormDataEsEntity : structureInsertList) {
            String esId = statisticPaasFormDataEsEntity.getEsId();
            setTeamLinkMap(aTeamLinkMap, esId, statisticPaasFormDataEsEntity.getAtdataId());
            setTeamLinkMap(bTeamLinkMap, esId, statisticPaasFormDataEsEntity.getBtdataId());
            setTeamLinkMap(cTeamLinkMap, esId, statisticPaasFormDataEsEntity.getCtdataId());
        }
        initTeamData(businessType, saasMark, biRelTableEnum, biProWarehouseEntity.getId(), aTeamLinkMap, FormLocationEnum.A);
        initTeamData(biProWarehouseEntity.getBtbusinessType(), biProWarehouseEntity.getBtsaasMark(), biRelTableEnum, biProWarehouseEntity.getId(), bTeamLinkMap,FormLocationEnum.B);
        if(hasSecondSlave){
            initTeamData(biProWarehouseEntity.getCtbusinessType(), biProWarehouseEntity.getCtsaasMark(),  biRelTableEnum, biProWarehouseEntity.getId(), cTeamLinkMap,FormLocationEnum.C);
        }
    }

    /**
     * 团队的处理
     * @param teamLinkMap
     * @param esId
     * @param dataId
     */
    private void setTeamLinkMap(Map<Long, Set<String>> teamLinkMap, String esId, Long dataId) {
        if (dataId != null) {
            Set<String> esIdSet;
            if (teamLinkMap.containsKey(dataId)) {
                esIdSet = teamLinkMap.get(dataId);
                if (esIdSet == null) {
                    esIdSet = new HashSet<>();
                }
            } else {
                esIdSet = new HashSet<>();
            }
            esIdSet.add(esId);
            teamLinkMap.put(dataId, esIdSet);
        }
    }

}
